Jump to content

If Statement Issues


SelfObscurity

Recommended Posts

Good Afternoon:

 

My attempt to use the switches for what I want to do is not working, so I'm going back to the if statements, but it's been a while.  Below is what I currently have, but it is not working.

 

<?php
if ($_GET['page'] == 'about')
{
$link = mysql_connect ($host, $user, $password);
  $query = "SELECT * from about";
  $result = mysql_db_query ($db, $query, $link);
  
  while ($row = mysql_fetch_array ($result)) {
echo ("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
       <tr>
         <td class=\"NewsHdr\">About Dangerous Alliance</td>
       </tr>
       <tr>
         <td class=\"NewsContent\">".$row['content'] . "</td>
       </tr>
     </table>");
  }
} 
if ($_GET['page'] == 'roster')
{
if ($_GET['id'])
{
	$id = $_GET['id'];
	$link = mysql_connect ($host, $user, $password);
  	$query = "SELECT * FROM roster WHERE id = '$id'";
  	$result = mysql_db_query ($db, $query, $link);
  
  	while ($row = mysql_fetch_array ($result)) {
	echo ("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
        <tr>
          <td colspan=\"8\" class=\"NewsHdr\">[-DA-] ".$row['name'] . "</td>
        </tr>
        <tr>
          <td width=\"12%\" class=\"NewsDetail\">Name</td>
          <td width=\"11%\" class=\"NewsDetail\">Rank</td>
          <td width=\"13%\" class=\"NewsDetail\">Status</td>
          <td width=\"12%\" class=\"NewsDetail\">SteamID</td>
          <td width=\"16%\" class=\"NewsDetail\">Allies</td>
          <td width=\"9%\" class=\"NewsDetail\">Axis</td>
          <td width=\"13%\" class=\"NewsDetail\">Contact</td>
          <td width=\"14%\" class=\"NewsDetail\">Recruitment Date</td>
        </tr>
        <tr>
          <td class=\"NewsContent\"><a href=\"index.php?page=roster&id=".$row['id'] . "\">".$row['name'] . "</a></td>
          <td class=\"NewsContent\">".$row['rank'] . "</td>
          <td class=\"NewsContent\">".$row['status'] . "</td>
          <td class=\"NewsContent\">".$row['steamid'] . "</td>
          <td class=\"NewsContent\">".$row['allies'] . "</td>
          <td class=\"NewsContent\">".$row['axis'] . "</td>
          <td class=\"NewsContent\">".$row['email'] . "</td>
          <td class=\"NewsContent\">".$row['recdate'] . "</td>
        </tr>
      </table>"); }
} else {
	$link = mysql_connect ($host, $user, $password);
  	$query = "SELECT * FROM roster WHERE approved = 0";
  	$result = mysql_db_query ($db, $query, $link);
  
  	while ($row = mysql_fetch_array ($result)) {
	echo ("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
        <tr>
          <td colspan=\"8\" class=\"NewsHdr\">Roster</td>
        </tr>
        <tr>
          <td width=\"12%\" class=\"NewsDetail\">Name</td>
          <td width=\"11%\" class=\"NewsDetail\">Rank</td>
          <td width=\"13%\" class=\"NewsDetail\">Status</td>
          <td width=\"12%\" class=\"NewsDetail\">SteamID</td>
          <td width=\"16%\" class=\"NewsDetail\">Allies</td>
          <td width=\"9%\" class=\"NewsDetail\">Axis</td>
          <td width=\"13%\" class=\"NewsDetail\">Contact</td>
          <td width=\"14%\" class=\"NewsDetail\">Recruitment Date</td>
        </tr>
        <tr>
          <td class=\"NewsContent\"><a href=\"index.php?page=roster&id=".$row['id'] . "\">".$row['name'] . "</a></td>
          <td class=\"NewsContent\">".$row['rank'] . "</td>
          <td class=\"NewsContent\">".$row['status'] . "</td>
          <td class=\"NewsContent\">".$row['steamid'] . "</td>
          <td class=\"NewsContent\">".$row['allies'] . "</td>
          <td class=\"NewsContent\">".$row['axis'] . "</td>
          <td class=\"NewsContent\">".$row['email'] . "</td>
          <td class=\"NewsContent\">".$row['recdate'] . "</td>
        </tr>
      </table>"); }
}
} else {
	$link = mysql_connect ($host, $user, $password);
  	$query = "SELECT * from news";
  	$result = mysql_db_query ($db, $query, $link);
  
  	while ($row = mysql_fetch_array ($result)) {
	echo ("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
        <tr>
          <td class=\"NewsHdr\">".$row['title'] . "</td>
        </tr>
        <tr>
          <td class=\"NewsDetail\">Posed by ".$row['author'] . " on ".$row['date'] . "</td>
        </tr>
        <tr>
          <td class=\"NewsContent\">".$row['content'] . "</td>
        </tr>
      </table>");
	}
}
}
?>

 

The problem is when I go to index.php?page=roster everything looks good.  When I click on a roster member, it goes to ?page=roster&id=1 which it should because thats their ID, but it doesn't load anything that I called for the if statement for $_GET['id'].

Link to comment
https://forums.phpfreaks.com/topic/195711-if-statement-issues/
Share on other sites

$link = mysql_connect ($host, $user, $password);
$query = "SELECT * FROM roster WHERE id = '$id'";
$result = mysql_db_query ($db, $query, $link);
while ($row = mysql_fetch_array ($result)) {

 

the output is dependant on your database calls being successful. there is no validation of this so it is possible it is causing a problem.

 

you can validate your database calls using if statements:

if ($link = mysql_connect ($host, $user, $password))
{
   $query = "SELECT * FROM roster WHERE id = '$id'";
   if ($result = mysql_db_query ($db, $query, $link))
   {
      while ($row = mysql_fetch_array ($result))
      {
         // etc.
      }
   }
   else
      print('Could not fetch a result from the database. Error given: '.mysql_error().'<br/>'."\n");
}
else
   print('Could not connect ot database. Error given: '.mysql_error().'<br/>'."\n");

 

if there is something going wrong there it could be a few different things. incorrect query syntax, wrong database address, wrong login details, query might simply be returning no results, and so on.

Link to comment
https://forums.phpfreaks.com/topic/195711-if-statement-issues/#findComment-1028202
Share on other sites

Hmm, maybe if I look at it section by section.

 

<?php
if ($_GET['page'] == 'roster')
{

This should state, if the URL is ?page=roster

 

if ($_GET['id'])
{
	$id = $_GET['id'];
	$link = mysql_connect ($host, $user, $password);

This should identify that if there is an additional variable on the IRL that is ?id=something.

 

$query = "SELECT * FROM roster WHERE id = '$id'";
  	$result = mysql_db_query ($db, $query, $link);

This should tell PHP to choose anything from the roster table, where the id = whatever is in the URL.

 

while ($row = mysql_fetch_array ($result)) {

// Blah Blah

If such is true, display whatever.

 

Am I misunderstanding something here or am I correct?

Link to comment
https://forums.phpfreaks.com/topic/195711-if-statement-issues/#findComment-1028213
Share on other sites

you are not misunderstanding and you are logically verifying everything in your head which is correct. but you are not allowing for a failing of the dependant data to follow the logic.

 

next step of debugging i would do is to trace the flow of the script using some small outputs:

if ($_GET['page'] == 'roster')
{
   if ($_GET['id'])
   {
      print('we are in id<br/>'."\n");
      $id = $_GET['id'];
      $link = mysql_connect ($host, $user, $password);
      $query = "SELECT * FROM roster WHERE id = '$id'";
      $result = mysql_db_query ($db, $query, $link);
      while ($row = mysql_fetch_array ($result)) 
      {
         print('we are in result loop<br/>'."\n");
         // etc
      }
} else {
   print('we are in id::else<br/>'."\n");
   // etc
}

 

this will let you see where your code is actually flowing when you execute it anmd give you clues on where the problem is starting.

Link to comment
https://forums.phpfreaks.com/topic/195711-if-statement-issues/#findComment-1028215
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.