Jump to content

[SOLVED] If Statement Not working as plan


yakk0

Recommended Posts

I'm trying to get it to display 5 latest piece of information added. But it seem to always display 2-3 and its not in order. Can anyone tell me what the problem may be?

 

<?php

 

include "config.php";

 

$query="SELECT * FROM data ORDER BY story_num ASC";

$result=mysql_query($query);

$num=mysql_numrows($result);

 

$i=0;

 

for ($i=0; $i<=5; $i++) {

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

 

$story_title=mysql_result($result,$i,"story_title");

$story_num=mysql_result($result,$i,"story_num");

$story_note=mysql_result($result,$i,"note");

 

if ($story_note==1)

{

echo "<img src='image/notice.png'> <a href='show_story.php?story_num=$story_num'><font color='white' size='2'>$story_title</font></a><br>";

$i++;

}

 

else if ($story_note==2)

{

echo "<img src='image/updated.png'> <a href='show_story.php?story_num=$story_num'><font color='white' size='2'>$story_title</font></a><br>";

$i++;

}

 

else if ($story_note==3)

{

echo "<img src='image/event.png'> <a href='show_story.php?story_num=$story_num'><font color='white' size='2'>$story_title</font></a><br>";

$i++;

}

 

else if ($story_note==4)

{

echo "<img src='image/important.png'> <a href='show_story.php?story_num=$story_num'><font color='white' size='2'>$story_title</font></a><br>";

$i++;

}

 

else

 

echo "<img src='image/notice.png'> <a href='show_story.php?story_num=$story_num'><font color='white' size='2'>$story_title</font></a><br>";

$i++;

 

}

}

mysql_close();

?>

Link to comment
https://forums.phpfreaks.com/topic/137207-solved-if-statement-not-working-as-plan/
Share on other sites

Try this:

 

<?php

include "config.php";

// putting a limit for the first 5 on the query
$query="SELECT * FROM data ORDER BY story_num ASC LIMIT 0,5";
$result=mysql_query($query);
$num=mysql_numrows($result);

// loop through the return
while($row = mysql_fetch_array($result)){
echo "<img src='images/notice.png'> <a href='show_story.php?stor_num={$row['story_num']}' style='color: #fff;font-size: 10pt;'>{$row['story_title']}</a><br />";
}

?>

 

Just to note, the issue with you code was that you where using "$i++" within the if statements, the for loop will automatically increment this number for you, so essentially each if statement was adding "2" to $i.

 

<?php
include "config.php";

$query="SELECT * FROM data ORDER BY story_num ASC";
$result=mysql_query($query);
$images = array(
  1 => 'image/notice.png',
  2 => 'image/updated.png',
  3 => 'image/event.png',
  4 => 'image/important.png',
);

for ($i=0; $row = mysql_fetch_array($result) && $i < 5; $i++)
{
  $story_title = $row['story_title'];
  $story_num   = $row['story_num'];
  $story_note  = $row['note'];
  $story_img   = isset($images[$story_note]) ? $images[$story_note] : $images[1];
  printf('<img src="%s"><a href="show_story.php?story_num=%s"><font color="white" size="2">%s</font></a><br>',$story_img,$story_num,$story_title);
}
?>

rhodesa the script you wrote out did list the 5, but there was no link visible. But thanks for trying.

 

cytech your script works fine, but you took out my if statement and now I'm back to square one with if statement. But thanks for fixing the list for 5.

 

Right now I'm trying to use $row['note'] but it doesn't seem to be working. Sorry I'm still new to learning PHP and SQL so hope you guys can assist me.

 

<?php

 

include "config.php";

 

// putting a limit for the first 5 on the query

$query="SELECT * FROM data ORDER BY story_num ASC LIMIT 0,5";

$result=mysql_query($query);

$num=mysql_numrows($result);

 

// loop through the return

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

 

$note = $row['note'];

 

if ($note == 1) {

 

echo "<img src='image/notice.png'> <a href='show_story.php?stor_num={$row['story_num']}' style='color: #fff;font-size: 10pt;'>{$row['story_title']}</a><br />";

 

}

 

elseif ($note == 2) {

 

echo "<img src='image/updated.png'> <a href='show_story.php?stor_num={$row['story_num']}' style='color: #fff;font-size: 10pt;'>{$row['story_title']}</a><br />";

 

}

 

else

 

echo "<img src='image/notice.png'> <a href='show_story.php?stor_num={$row['story_num']}' style='color: #fff;font-size: 10pt;'>{$row['story_title']}</a><br />";

 

}

 

}

 

?>

 

 

it is not finding any data to display. or rather...it's finding the rows, but the data isn't being assigned to $row.

 

are you sure you copied the code correctly that i posted? specifically this line:

for ($i=0; $row = mysql_fetch_array($result) && $i < 5; $i++)

try this and see what happens:

<?php
include "config.php";

$query="SELECT * FROM data ORDER BY story_num ASC";
$result=mysql_query($query);
$images = array(
  1 => 'image/notice.png',
  2 => 'image/updated.png',
  3 => 'image/event.png',
  4 => 'image/important.png',
);

for ($i=0; $row = mysql_fetch_array($result) && $i < 5; $i++)
{
  $story_title = $row['story_title'];
  $story_num   = $row['story_num'];
  $story_note  = $row['note'];
  $story_img   = isset($images[$story_note]) ? $images[$story_note] : $images[1];
  printf('<img src="%s"><a href="show_story.php?story_num=%s"><font color="white" size="2">%s</font></a><br>',$story_img,$story_num,$story_title);
  print '<pre>'.print_r($row,1).'</pre>';
}
?>

Now there is a number 1 on each line, but its right under the notice image. Also still no link. Also I just want to say I think the array for displaying the images isn't working correctly either. I don't think they are all suppose to display notice. Sorry for making this so hard.

col_list.php

 

<table border='0' width='190'>
<tr><td><?php
include "config.php";

$query="SELECT * FROM data ORDER BY story_num ASC";
$result=mysql_query($query);
$images = array(
  1 => 'image/notice.png',
  2 => 'image/updated.png',
  3 => 'image/event.png',
  4 => 'image/important.png',
);

for ($i=0; $row = mysql_fetch_array($result) && $i < 5; $i++)
{
  $story_title = $row['story_title'];
  $story_num   = $row['story_num'];
  $story_note  = $row['note'];
  $story_img   = isset($images[$story_note]) ? $images[$story_note] : $images[1];
  printf('<img src="%s"><a href="show_story.php?story_num=%s"><font color="white" size="2">%s</font></a><br>',$story_img,$story_num,$story_title);
  print '<pre>'.print_r($row,1).'</pre>';
}
?></tr></td>
</table>

 

show_all.php

 

<?php include "config.php" ?>
<html>
<head>
<title><?PHP echo "$site_name" ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--

a:link {  color: #FFFFFF; text-decoration: none}

a:visited {  text-decoration: none; color: #FFCC66}

a:hover {  color: #99CCFF; text-decoration: underline}
body {  font-family: "Arial", "Helvetica", "sans-serif"}

-->
</style>
</head>

<html>
<head>
<title>RG3</title>
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="1000" cellspacing="0" cellpadding="0" height="100%">
<tr>
  <td valign="top"><table width="34" height="100%" cellspacing="0" cellpadding="0" bgcolor="#0ab8ff">
   <tr>
    <td valign="top" background="../images/RG3_01.png" width="34" height="496"></td>
   </tr>
    <tr>
     <td valign="top" bgcolor="#0ab8ff" width="34" height="100%"></td>
    </tr>
   </table></td>
   <td valign="top"><table width="356" height="100%" cellspacing="0" cellpadding="0" bgcolor="#0ab8ff">
   <tr>
    <td valign="top" background="../images/RG3_02.png" width="356" height="4"></td>
   </tr>
     <tr>
    <td valign="top" background="../images/RG3_06.png" width="356" height="305"></td>
   </tr>
     <tr>
    <td valign="top" background="../images/RG3_18.png" width="356" height="25"></td>
   </tr>
    <tr>
<//////////// Latest News /////////////>
<td valign="top" width="356"><table width="356" height="197" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" background="../images/RG3_19.png" width="4" height="197"></td>
<td valign="top" background="../images/RG3_20.png" width="348" height="197">
<//////////// Latest News Script Goes Here /////////////>	


<//////////// Latest News Script Ends/////////////>	
</td>
<td valign="top" background="../images/RG3_21.png" width="4" height="197"></td>
</tr>
</table></td>
   </tr>
    <tr>
    <td valign="top" background="../images/RG3_25.png" width="356" height="13"></td>
   </tr>
       <tr>
    <td valign="top" background="../images/RG3_26.png" width="356" height="10"></td>
   </tr>
       <tr>
    <td valign="top" background="../images/RG3_27.png" width="356" height="25"></td>
   </tr>
       <tr>
<//////////// Top 100 News /////////////>
<td valign="top" width="356"><table width="356" height="199" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" background="../images/RG3_28.png" width="4" height="199"></td>
<td valign="top" background="../images/RG3_29.png" width="348" height="197">
<//////////// Top 100 Script Goes Here /////////////>	


<//////////// Top 100 Script Ends/////////////>	
</td>
<td valign="top" background="../images/RG3_30.png" width="4" height="199"></td>
</tr>
</table></td>
   </tr>
     <tr>
    <td valign="top" background="../images/RG3_31.png" width="356" height="11"></td>
   </tr>
        <tr>
    <td valign="top" bgcolor="#0ab8ff" width="356" height="100%"></td>
   </tr>
   </table></td>
   <td valign="top"><table width="10" height="100%" cellpadding="0" cellspacing="0">
   <tr>
   <td valign="top" width="10" height="496" background="../images/RG3_03.png"></td>
   </tr>
     <tr>
    <td valign="top" bgcolor="#0ab8ff" width="10" height="100%"></td>
   </tr>
   </table></td>
      <td valign="top"><table width="10" height="100%" cellpadding="0" cellspacing="0">
   <tr>
   <td valign="top" width="574" height="4" background="../images/RG3_04.png"></td>
   </tr>
      <tr>
   <td valign="top" width="574"><table width="574" cellspacing="0" cellpadding="0">
   <tr>
   <td valign="top"><a href="http://www.javascriptkit.com" onMouseover="showit(0)"><img src="../images/RG3_07.png" border="0"></a></td>
    <td valign="top"><a href="http://www.javascriptkit.com" onMouseover="showit(1)"><img src="../images/RG3_08.png" border="0"></a></td>
 <td valign="top"><a href="http://www.javascriptkit.com" onMouseover="showit(2)"><img src="../images/RG3_09.png" border="0"></a></td>
  <td valign="top"><a href="http://www.javascriptkit.com" onMouseover="showit(3)"><img src="../images/RG3_10.png" border="0"></a></td>
   <td valign="top"><a href="http://www.javascriptkit.com"><img src="../images/RG3_11.png" border="0"></a></td>
    <td valign="top"><a href="http://www.javascriptkit.com"><img src="../images/RG3_12.png" border="0"></a></td>
   </tr>
   
   </table></td>
   </tr>
   <tr>
   <td valign="top" background="../images/RG3_13.png" width="574" height="122">
<ilayer width=550 height=100 name="dep1">
<layer name="dep2" width=550 height=100>
</layer>
</ilayer>
<div id="describe" ;width:400px;height:32px" onMouseover="clear_delayhide()" onMouseout="resetit(event)"></div>
   
  
<script language="JavaScript1.2">

var submenu=new Array()

submenu[0]='<font size="1" face="Verdana"><b><a href="#">Asda Story News</a> | <a href="#">Asda Story Events</a> | <a href="#">RG News</a> | <a href="#">RG Events</a></b></font>'

submenu[1]='<font size="1" face="Verdana"><b><a href="#">Beginners Guide</a> | <a href="#">Classes</a> | <a href="#">Soulmate System</a> | <a href="#">Crafting System</a> | <a href="#">Trade System</a> | <a href="#">Titles</a> | <a href="#">Accessories</a>  <a href="#">Monsters</a> | <a href="#">Quest</a> | <a href="#">PVP</a></b></font>'

submenu[2]='<font size="1" face="Verdana"><b><a href="#">Realm Guardians</a> | <a href="#">Zenith</a> | <a href="#">Why So Serious?</a> | <a href="#">TFT</a> | <a href="#">Top 100 Players (Alliance)</a> </b></font>'

submenu[3]='<font size="1" face="Verdana"><b><a href="#">Realm Guardians</a> | <a href="#">Zenith</a> | <a href="#">Why So Serious?</a> | <a href="#">TFT</a> </b></font>'


var delay_hide=500

var menuobj=document.getElementById? document.getElementById("describe") : document.all? document.all.describe : document.layers? document.dep1.document.dep2 : ""

function showit(which){
clear_delayhide()
thecontent=(which==-1)? "" : submenu[which]
if (document.getElementById||document.all)
menuobj.innerHTML=thecontent
else if (document.layers){
menuobj.document.write(thecontent)
menuobj.document.close()
}
}

function resetit(e){
if (document.all&&!menuobj.contains(e.toElement))
delayhide=setTimeout("showit(-1)",delay_hide)
else if (document.getElementById&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
delayhide=setTimeout("showit(-1)",delay_hide)
}

function clear_delayhide(){
if (window.delayhide)
clearTimeout(delayhide)
}

function contains_ns6(a, b) {
while (b.parentNode)
if ((b = b.parentNode) == a)
return true;
return false;
}

</script>  
   
   </td>
   </tr>
        <tr>
    <td valign="top" background="../images/RG3_14.png" width="574" height="16"></td>
   </tr>
     <tr>
    <td valign="top" width="574" height="100%"><table width="574" height="100%" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" background="../images/RG3_15.png" height="100%" width="4"></td>
<td valign="top" bgcolor="#4f3a2a" height="100%" width="566">
</////////EVERYTHING GOES HERE///////>

<?php include "col_list.php" ?>

</////////EVERYTHING ENDS HERE///////>
</td>
<td valign="top" background="../images/RG3_17.png" height="100%" width="4"></td>
</tr>
</table>
</td>
   </tr>
        <tr>
    <td valign="top" background="../images/RG3_33.png" width="574" height="17"></td>
   </tr>
     <tr>
    <td valign="top" bgcolor="#0ab8ff" width="10" height="100%"></td>
   </tr>
   </table></td>
   <td valign="top"><table width="76" height="100%" cellspacing="0" cellpadding="0">
   <tr>
    <td valign="top" background="../images/RG3_05.png" width="76" height="496"></td>
   </tr>
      <tr>
    <td valign="top" bgcolor="#0ab8ff" width="76" height="100%"></td>
   </tr>
</tr>
</table>

it just puzzles me....ok...let's try this:

<table border='0' width='190'>
<tr><td><?php
require_once("config.php");

$query="SELECT * FROM data ORDER BY story_num ASC";
$result=mysql_query($query) or die(mysql_error());
$images = array(
  1 => 'image/notice.png',
  2 => 'image/updated.png',
  3 => 'image/event.png',
  4 => 'image/important.png',
);

for ($i=0; $i < 5 && $i < mysql_num_rows($result); $i++)
{
  $row = mysql_fetch_assoc($result);
  print '<pre>'.print_r($row,1).'</pre>';
  $story_title = $row['story_title'];
  $story_num   = $row['story_num'];
  $story_note  = $row['note'];
  $story_img   = isset($images[$story_note]) ? $images[$story_note] : $images[1];
  printf('<img src="%s"><a href="show_story.php?story_num=%s"><font color="white" size="2">%s</font></a><br>',$story_img,$story_num,$story_title);
}
?></tr></td>
</table>

<table border='0' width='190'>
<tr><td><?php
require_once("config.php");

$query="SELECT * FROM data ORDER BY story_num ASC";
$result=mysql_query($query) or die(mysql_error());
$images = array(
  1 => 'image/notice.png',
  2 => 'image/updated.png',
  3 => 'image/event.png',
  4 => 'image/important.png',
);

for ($i=0; $i < 5 && $i < mysql_num_rows($result); $i++)
{
  $row = mysql_fetch_assoc($result);
  $story_title = $row['story_title'];
  $story_num   = $row['story_num'];
  $story_note  = $row['note'];
  $story_img   = isset($images[$story_note]) ? $images[$story_note] : $images[1];
  printf('<img src="%s"><a href="show_story.php?story_num=%s"><font color="white" size="2">%s</font></a><br>',$story_img,$story_num,$story_title);
}
?></tr></td>
</table>

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.