Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if ($i=0) problem?? 1

Status
Not open for further replies.

hisham

IS-IT--Management
Nov 6, 2000
194
I have in mysql database 2 tables: category and items
I used the following code to display the first row of the “name” field from category table:

$query = "SELECT * FROM category";
$result = mysql_query($query);

$id = mysql_result($result,0,"name");
$idd = mysql_result($result,0,"id");
?>
<table>
<td>
echo &quot;<p align=\&quot;center\&quot;><a href=\&quot;details.php?sid=$idd\&quot;>$id</a>&quot;;
?>
</td></table>

but I tried to modify the code to display only 2 rows “ the number of rows is 18 ” in the same page by using the following code, but still display the first row which is the same result “as the above”:

$query = &quot;SELECT * FROM category&quot;;
$result = mysql_query($query);

$id = mysql_result($result,$i,&quot;name&quot;);
$idd = mysql_result($result,$i,&quot;id&quot;);
?>
<table>
<td>
<? if ($i=0) {
echo &quot;<p align=\&quot;center\&quot;><a href=\&quot;articles.php?sid=$idd\&quot;>$id</a>&quot;;
}
?>
</td></table>
// here some html
<table>
<td>
<? if ($i=1) {
echo &quot;<p align=\&quot;center\&quot;><a href=\&quot;articles.php?sid=$idd\&quot;>$id</a>&quot;;
}
?>
</td></table>
 
In your if statements, you use the assignment operator when you should use the == operator. Cahnge it and it should work. //Daniel
 
Thanks Daniel, i used: if ($i==1) but still display the same result and i don't understand why !!

the only way i found is:
<table><td>
<?php $id=mysql_result($result,0,&quot;naam&quot;);
$idd = mysql_result($result,0,&quot;id&quot;);
echo &quot;<p align=\&quot;center\&quot;><a href=\&quot;articles.php?sid=$idd\&quot;>$id</a>&quot;; ?>
</td><table>
<table><td>
<?php $id=mysql_result($result,1,&quot;naam&quot;);
$idd = mysql_result($result,1,&quot;id&quot;);
echo &quot;<p align=\&quot;center\&quot;><a href=\&quot;articles.php?sid=$idd\&quot;>$id</a>&quot;; ?>
</td><table>
so allways i need to write MySQL Function: mysql_result &quot;$id and $idd&quot; in every <td> with changing the value of the int row of this function. I think this is not the perfect way to do that !!


 
Well, you could use a for loop.
Code:
<table>
<?php
for ($i = 0; $i < 2; $i++)
{
    $naam = mysql_result($result, $i, &quot;naam&quot;);
    $id = mysql_result($result, $i, &quot;id&quot;);
    echo &quot;<tr><td><p align=\&quot;center\&quot;><a href=\&quot;articles.php?sid=$id\&quot;>$naam</a></p></td></tr>&quot;;
}
?>
</table>
//Daniel
 
Thank you, it works now, and i use to display every value in different <table> in the page the following:

<table>
<?php
if ($i = 1)
{
$naam = mysql_result($result, $i, &quot;naam&quot;);
$id = mysql_result($result, $i, &quot;id&quot;);
echo &quot;<tr><td><p align=\&quot;center\&quot;><a href=\&quot;articles.php?sid=$id\&quot;>$naam</a></p></td></tr>&quot;;
}
?>
</table>
<table>
<?php
if ($i = 2)
{
$naam = mysql_result($result, $i, &quot;naam&quot;);
$id = mysql_result($result, $i, &quot;id&quot;);
echo &quot;<tr><td><p align=\&quot;center\&quot;><a href=\&quot;articles.php?sid=$id\&quot;>$naam</a></p></td></tr>&quot;;
}
?>
</table>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top