TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-03-2005, 12:59 PM   #1 (permalink)
The Wanderer
 
Join Date: May 2005
Location: Maine|USA
Posts: 17
Thanks: 0
Ogden2k is on a distinguished road
Default Multiple If satements

I have a php file that has an if statement... If this link is clicked, it will show a web form and not show anything else. If the link is not clicked the data will be shown.

Now, I want to have another link that will load only a certain type of data, basically sorting the MySQL information a certain way. I'm not sure how to implement this.

Here's the current code that I have now:
PHP Code:
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Games Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel="stylesheet" type="text/css" href="http://misc.techiehq.net/stuff/css/games.css" />
</head>
<body>
<div class="body_fore">
<?php
if (isset($_GET['addgame'])): // User wants to add a Xbox Title
?>
<div class="title">Submit a game</div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>
Please enter the following information:<br /><br />
<table>
<tr>
    <td>System:</td><td><select name="system"><optgroup label="Game Systems:"><option value="Game Cube" name="Cube">Game Cube</option><option value="PC" name="PC">PC</option><option value="Xbox" name="Xbox">Xbox</option></optgroup></select></td>
</tr>
<tr>
    <td>Title:</td><td><input class="forms" type="text" size="50" maxlength="50" name="title" /></td>
</tr>
<tr>
    <td>Developer:</td><td><input class="forms" type="text" size="50" maxlength="50" name="developer" /><td>
</tr>
<tr>
    <td>Publisher:</td><td><input class="forms" type="text" size="50" maxlength="50" name="publisher" /></td>
</tr>
<tr>
    <td>Year:</td><td><input class="forms" value="0000-00-00" type="text" size="50" maxlength="50" name="year" /><td>
</tr>
<tr>
    <td>Purchased:</td><td><input class="forms" value="0000-00-00" type="text" size="50" maxlength="50" name="purchased" /><td>
</tr>
<tr>
    <td>Genre:</td><td><input class="forms" type="text" size="50" maxlength="50" name="genre" /><td>
</tr>
<tr>
    <td>ISBN:</td><td><input class="forms" type="int" size="50" maxlength="50" name="ISBN" /><td>
</tr>
<tr>
    <td>Serial:</td><td><input class="forms" type="int" size="50" maxlength="50" name="serial" /><td>
</tr> 
</table>
</label>
<br /><input type="submit" value="Submit" />
</form>
<?php else: // Home page display
// Connect to the DB server
$dbcnx = @mysql_connect('localhost''''');
if (!
$dbcnx) {
exit(
'<p>Unable to connect to the ' .
'database server at this time.</p>');
}
// Select the binarydr_stuff DB
if (!@mysql_select_db('binarydr_stuff')) {
exit(
'<p>Unable to locate the stuff ' .
'database at this time.</p>');
}
if (isset(
$_POST['title'])) { //Check for a field that is mandatory to do the insert 
 
$system mysql_escape_string($_POST['system']);
$title mysql_escape_string($_POST['title']); 
$publisher mysql_escape_string($_POST['publisher']); 
$year mysql_escape_string($_POST['year']);
$purchased mysql_escape_string($_POST['purchased']);
$developer mysql_escape_string($_POST['developer']);
$genre mysql_escape_string($_POST['genre']);    
$ISBN mysql_escape_string($_POST['ISBN']);
$serial mysql_escape_string($_POST['serial']);
$sql "INSERT INTO games (system, title, publisher, year, purchased, developer, genre, ISBN, serial) 
             VALUES('
$system','$title','$publisher','$year','$purchased','$developer','$genre','$ISBN','$serial')"
mysql_query($SQL); 
if (@
mysql_query($sql)) {
echo 
'<em>Your data has been added</em><br />';
} else {
echo 
'<p>Error adding submitted info: ' mysql_error(). '</p>';
}
}
echo 
'<div class="title">Games:</div>
<table class="table_head">
<tr>
     <td width="85">System</td><td width="200">Title</td><td width="110">Developer</td><td width="140">Publisher</td><td width="140">Year</td><td width="140">Purchased</td><td width="88">Genre</td><td width="120">Serial</td>
</tr>
</table>
'
;
// Request the text of all the info
$result = @mysql_query('SELECT * FROM games');
if (!
$result) {
exit(
'<p>Error performing query: ' mysql_error() . '</p>');
}
// Display the text of each game in a row
while ($row mysql_fetch_array($result)) {
echo 
'<table class="cells">
<tr>
     <td width="85">' 
$row['system'] . '</td><td width="200">' $row['title'] . '</td><td width="110">' $row['developer'] . '</td><td width="140">' $row['publisher'] . '</td><td width="140">' $row['year'] . '</td><td width="140">' $row['purchased'] . '</td><td width="88">' $row['genre'] .'</td><td width="120">' $row['serial'] .'</td>
</tr>
</table>
'
;
}
// When clicked, this link will load this page with the hook sub form displayed
echo '<div class="button_area">
<p>[ <a href="' 
$_SERVER['PHP_SELF'] . '?addgame=1">Add a Game</a> | <a href="search.php">Search</a> | <a href="../">Home</a> | <a href="' $_SERVER['PHP_SELF'] . '">Refresh</a> ]</p></div>
</div>
</div>'
;
endif;
?>
</body>
</html>
__________________
TechieHQ
Send a message via AIM to Ogden2k
Ogden2k is offline  
Reply With Quote
Old 06-03-2005, 01:04 PM   #2 (permalink)
The Acquainted
 
Join Date: May 2005
Posts: 106
Thanks: 0
jaswinder_rana is on a distinguished road
Default

ok here's the whole idea, if you won't get it then ask again to explain in full.
NOTE: i would prefer not to use
if():
endif;
but, i would prefer
if()
{}

the second approach is more clear
PHP Code:
if (isset($_GET['addgame']))
{
// do the form to add game
}
elseif(if (isset(
$_GET['playgame']))
{
  
//show the code toplay the game
}
elseif(isset(
$_GET['delgame']))
{
 
//Delete the game
}
else
{
  
// Home page display

i think you got the idea. again feel free to clear any misunderstandings.
__________________
---------------------------
Errors = Improved Programming.
Portfolio
Send a message via MSN to jaswinder_rana
jaswinder_rana is offline  
Reply With Quote
Old 06-03-2005, 06:43 PM   #3 (permalink)
The Wanderer
 
Join Date: May 2005
Location: Maine|USA
Posts: 17
Thanks: 0
Ogden2k is on a distinguished road
Default

I think I understand what you're saying, but I can't get it to implement right.

Basically, what I'm trying to do is take the code I have now, add another IF statement, so when the search link is clicked, it will reload index.php but only show the search HTML portion, just like it does with the game submission form.
__________________
TechieHQ
Send a message via AIM to Ogden2k
Ogden2k is offline  
Reply With Quote
Old 06-03-2005, 06:48 PM   #4 (permalink)
The Acquainted
 
Join Date: May 2005
Posts: 106
Thanks: 0
jaswinder_rana is on a distinguished road
Default

sure just add the following lines BEFORE THE ELSE STATEMENT

PHP Code:
elseif($_GET['search']):
  echo 
'here is the form, please fill it out 
Make sure add these lines BEFORE THE ELSE(there are many elses, what i think you got the idea, if not feel free to ask)
AND
replace the echo statement with your search form
__________________
---------------------------
Errors = Improved Programming.
Portfolio
Send a message via MSN to jaswinder_rana
jaswinder_rana is offline  
Reply With Quote
Old 06-03-2005, 07:04 PM   #5 (permalink)
The Wanderer
 
Join Date: May 2005
Location: Maine|USA
Posts: 17
Thanks: 0
Ogden2k is on a distinguished road
Default

I'm still confused, I tried a few different layouts and wasn't able to get this to work. Here's what I currently have for code that works, at the top you can see that the
<?php
if (isset($_GET['addgame'])): // User wants to add a Xbox Title
?>
statement is where it only shows the add form, then after the add form HTML, there's an if statement that loads the actual PHP code for the rest of the site.

PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Games Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel="stylesheet" type="text/css" href="http://misc.techiehq.net/stuff/css/games.css" />
</head>
<body>
<div class="body_fore">
<?php
if (isset($_GET['addgame'])): // User wants to add a Xbox Title
?>
<div class="title">Submit a game</div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>
Please enter the following information:<br /><br />
<table>
<tr>
    <td>System:</td><td><select name="system"><optgroup label="Game Systems:"><option value="Game Cube" name="Cube">Game Cube</option><option value="PC" name="PC">PC</option><option value="Xbox" name="Xbox">Xbox</option></optgroup></select></td>
</tr>
<tr>
    <td>Title:</td><td><input class="forms" type="text" size="50" maxlength="50" name="title" /></td>
</tr>
<tr>
    <td>Developer:</td><td><input class="forms" type="text" size="50" maxlength="50" name="developer" /><td>
</tr>
<tr>
    <td>Publisher:</td><td><input class="forms" type="text" size="50" maxlength="50" name="publisher" /></td>
</tr>
<tr>
    <td>Year:</td><td><input class="forms" value="0000-00-00" type="text" size="50" maxlength="50" name="year" /><td>
</tr>
<tr>
    <td>Purchased:</td><td><input class="forms" value="0000-00-00" type="text" size="50" maxlength="50" name="purchased" /><td>
</tr>
<tr>
    <td>Genre:</td><td><input class="forms" type="text" size="50" maxlength="50" name="genre" /><td>
</tr>
<tr>
    <td>ISBN:</td><td><input class="forms" type="int" size="50" maxlength="50" name="ISBN" /><td>
</tr>
<tr>
    <td>Serial:</td><td><input class="forms" type="int" size="50" maxlength="50" name="serial" /><td>
</tr> 
</table>
</label>
<br /><input type="submit" value="Submit" />
</form>
<?php else: // Home page display
// Connect to the DB server
$dbcnx = @mysql_connect('localhost''''');
if (!
$dbcnx) {
exit(
'<p>Unable to connect to the ' .
'database server at this time.</p>');
}
// Select the binarydr_stuff DB
if (!@mysql_select_db('binarydr_stuff')) {
exit(
'<p>Unable to locate the stuff ' .
'database at this time.</p>');
}
if (isset(
$_POST['title'])) { //Check for a field that is mandatory to do the insert 
 
$system mysql_escape_string($_POST['system']);
$title mysql_escape_string($_POST['title']); 
$publisher mysql_escape_string($_POST['publisher']); 
$year mysql_escape_string($_POST['year']);
$purchased mysql_escape_string($_POST['purchased']);
$developer mysql_escape_string($_POST['developer']);
$genre mysql_escape_string($_POST['genre']);    
$ISBN mysql_escape_string($_POST['ISBN']);
$serial mysql_escape_string($_POST['serial']);
$sql "INSERT INTO games (system, title, publisher, year, purchased, developer, genre, ISBN, serial) 
             VALUES('
$system','$title','$publisher','$year','$purchased','$developer','$genre','$ISBN','$serial')"
mysql_query($SQL); 
if (@
mysql_query($sql)) {
echo 
'<em>Your data has been added</em><br />';
} else {
echo 
'<p>Error adding submitted info: ' mysql_error(). '</p>';
}
}
echo 
'<div class="title">Games:</div>
<table class="table_head">
<tr>
     <td width="85">System ^ v</td>
     <td width="200">Title ^ v</td>
     <td width="110">Developer ^ v</td>
     <td width="140">Publisher ^ v</td>
     <td width="140">Year ^ v</td>
     <td width="140">Purchased ^ v</td>
     <td width="88">Genre ^ v</td>
     <td width="120">Serial ^ v</td>
</tr>
</table>
'
;
// Request the text of all the info
$result = @mysql_query('SELECT * FROM games');
if (!
$result) {
exit(
'<p>Error performing query: ' mysql_error() . '</p>');
}
// Display the text of each game in a row
while ($row mysql_fetch_array($result)) {
echo 
'
<table class="cells">
<tr>
     <td width="85">' 
$row['system'] . '</td>
     <td width="200">' 
$row['title'] . '</td>
     <td width="110">' 
$row['developer'] . '</td>
     <td width="140">' 
$row['publisher'] . '</td>
     <td width="140">' 
$row['year'] . '</td>
     <td width="140">' 
$row['purchased'] . '</td>
     <td width="88">' 
$row['genre'] .'</td>
     <td width="120">' 
$row['serial'] .'</td>
</tr>
</table>
'
;
}
// When clicked, this link will load this page with the hook sub form displayed
echo '<div class="button_area">
<p>[ <a href="' 
$_SERVER['PHP_SELF'] . '?addgame=1">Add a Game</a> | <a href="search.php">Search</a> | <a href="../">Home</a> | <a href="' $_SERVER['PHP_SELF'] . '">Refresh</a> ]</p></div>
</div>
</div>'
;
endif;
?>
</body>
</html>
__________________
TechieHQ
Send a message via AIM to Ogden2k
Ogden2k is offline  
Reply With Quote
Old 06-03-2005, 07:14 PM   #6 (permalink)
The Acquainted
 
Join Date: May 2005
Posts: 106
Thanks: 0
jaswinder_rana is on a distinguished road
Default

ok, first to make sure
1) if addgame is in the url means show the form
2) when the form is submitted then enter the info in database and show the info

Now you wanna add
3) if, say, search is in the url then search the database. if i am right then heres the code which will work
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Games Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel="stylesheet" type="text/css" href="http://misc.techiehq.net/stuff/css/games.css" />
</head>
<body>
<div class="body_fore">
<?php
if (isset($_GET['addgame'])): // User wants to add a Xbox Title
?>
<div class="title">Submit a game</div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>
Please enter the following information:<br /><br />
<table>
<tr>
    <td>System:</td><td><select name="system"><optgroup label="Game Systems:"><option value="Game Cube" name="Cube">Game Cube</option><option value="PC" name="PC">PC</option><option value="Xbox" name="Xbox">Xbox</option></optgroup></select></td>
</tr>
<tr>
    <td>Title:</td><td><input class="forms" type="text" size="50" maxlength="50" name="title" /></td>
</tr>
<tr>
    <td>Developer:</td><td><input class="forms" type="text" size="50" maxlength="50" name="developer" /><td>
</tr>
<tr>
    <td>Publisher:</td><td><input class="forms" type="text" size="50" maxlength="50" name="publisher" /></td>
</tr>
<tr>
    <td>Year:</td><td><input class="forms" value="0000-00-00" type="text" size="50" maxlength="50" name="year" /><td>
</tr>
<tr>
    <td>Purchased:</td><td><input class="forms" value="0000-00-00" type="text" size="50" maxlength="50" name="purchased" /><td>
</tr>
<tr>
    <td>Genre:</td><td><input class="forms" type="text" size="50" maxlength="50" name="genre" /><td>
</tr>
<tr>
    <td>ISBN:</td><td><input class="forms" type="int" size="50" maxlength="50" name="ISBN" /><td>
</tr>
<tr>
    <td>Serial:</td><td><input class="forms" type="int" size="50" maxlength="50" name="serial" /><td>
</tr>
</table>
</label>
<br /><input type="submit" value="Submit" />
</form>
<?php 
/*
 HERE STARTS MY CODE

change $_GET['search'] to whichever value you will use in the url
*/
elseif(isset($_GET['search'])):

<
form>
<
input type="text" name="search" value="Search Database">
</
form>

/*
  HERE ENDS MY CODE
*/
else: // Home page display
// Connect to the DB server
$dbcnx = @mysql_connect('localhost''''');
if (!
$dbcnx) {
exit(
'<p>Unable to connect to the ' .
'database server at this time.</p>');
}
// Select the binarydr_stuff DB
if (!@mysql_select_db('binarydr_stuff')) {
exit(
'<p>Unable to locate the stuff ' .
'database at this time.</p>');
}
if (isset(
$_POST['title'])) { //Check for a field that is mandatory to do the insert

$system mysql_escape_string($_POST['system']);
$title mysql_escape_string($_POST['title']);
$publisher mysql_escape_string($_POST['publisher']);
$year mysql_escape_string($_POST['year']);
$purchased mysql_escape_string($_POST['purchased']);
$developer mysql_escape_string($_POST['developer']);
$genre mysql_escape_string($_POST['genre']);    
$ISBN mysql_escape_string($_POST['ISBN']);
$serial mysql_escape_string($_POST['serial']);
$sql "INSERT INTO games (system, title, publisher, year, purchased, developer, genre, ISBN, serial)
             VALUES('
$system','$title','$publisher','$year','$p  urchased','$developer','$genre','$ISBN','$serial')  ";
mysql_query($SQL);
if (@
mysql_query($sql)) {
echo 
'<em>Your data has been added</em><br />';
} else {
echo 
'<p>Error adding submitted info: ' mysql_error(). '</p>';
}
}
echo 
'<div class="title">Games:</div>
<table class="table_head">
<tr>
     <td width="85">System ^ v</td>
     <td width="200">Title ^ v</td>
     <td width="110">Developer ^ v</td>
     <td width="140">Publisher ^ v</td>
     <td width="140">Year ^ v</td>
     <td width="140">Purchased ^ v</td>
     <td width="88">Genre ^ v</td>
     <td width="120">Serial ^ v</td>
</tr>
</table>
'
;
// Request the text of all the info
$result = @mysql_query('SELECT * FROM games');
if (!
$result) {
exit(
'<p>Error performing query: ' mysql_error() . '</p>');
}
// Display the text of each game in a row
while ($row mysql_fetch_array($result)) {
echo 
'
<table class="cells">
<tr>
     <td width="85">' 
$row['system'] . '</td>
     <td width="200">' 
$row['title'] . '</td>
     <td width="110">' 
$row['developer'] . '</td>
     <td width="140">' 
$row['publisher'] . '</td>
     <td width="140">' 
$row['year'] . '</td>
     <td width="140">' 
$row['purchased'] . '</td>
     <td width="88">' 
$row['genre'] .'</td>
     <td width="120">' 
$row['serial'] .'</td>
</tr>
</table>
'
;
}
// When clicked, this link will load this page with the hook sub form displayed
echo '<div class="button_area">
<p>[ <a href="' 
$_SERVER['PHP_SELF'] . '?addgame=1">Add a Game</a> | <a href="search.php">Search</a> | <a href="../">Home</a> | <a href="' $_SERVER['PHP_SELF'] . '">Refresh</a> ]</p></div>
</div>
</div>'
;
endif;
?>
</body>
</html>
hope this helps
__________________
---------------------------
Errors = Improved Programming.
Portfolio
Send a message via MSN to jaswinder_rana
jaswinder_rana is offline  
Reply With Quote
Old 06-03-2005, 07:51 PM   #7 (permalink)
The Wanderer
 
Join Date: May 2005
Location: Maine|USA
Posts: 17
Thanks: 0
Ogden2k is on a distinguished road
Default

Great, I got it working. Thank you! Now I understand after seeing the code implemented.

I think later on, submit a game and search will have a separate files / pages.
__________________
TechieHQ
Send a message via AIM to Ogden2k
Ogden2k is offline  
Reply With Quote
Old 06-03-2005, 07:58 PM   #8 (permalink)
The Acquainted
 
Join Date: May 2005
Posts: 106
Thanks: 0
jaswinder_rana is on a distinguished road
Default

Seperating pages in to different pages is a good thing. its easy to handle them.
OR
you have tohe option to do it right now, WITHOUT CHANGING any thing. here's the idea

add.php
PHP Code:
//<form>
//INPUT fields  here
//</form> 
search.php
PHP Code:
//<form>
//SEARCH fields here
//</form> 
show.php
PHP Code:
//connect to database and show results here 
main.php [THIS IS THE MAAIN PAGE]
PHP Code:
<DOCTYPE....
blah blah

if(isset($_GET['addme'])):
 include(
'add.php');
elseif(isset(
$_GET['search']))
  include(
'search.php');
else:
 include(
'show.php'); 
if you just paste the code(in the if statements) in the appropriate pages. it'll work without doing anything extra. it'll look more professional and it'll be easy to handle

hope this helps

EDIT: again i prefer to use {} in if statements rather than : {} are just easy to read and notice, coz you always know where it'll end
__________________
---------------------------
Errors = Improved Programming.
Portfolio
Send a message via MSN to jaswinder_rana
jaswinder_rana is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:15 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design