0

I tried searching but didn't find a good answer to my question. I have the following code for a loop:

<?php
$i=0;
while ($i < $num) {

$f0=mysql_result($sql,$i,"id");
$f1=mysql_result($sql,$i,"title");
$f2=mysql_result($sql,$i,"post");
?>

<p><?php echo $f1; ?></p>
<p><?php echo $f2; ?></p>
<p>Categories:</p>

<?php
$i++;
}
?>

<?php
$i=0;
while ($i < $num1) {
$f3=mysql_result($sql1,$i,"category");
?>

<?php echo $f3; ?>

<a href="edit.php?id=<?php echo $f0; ?>"><button>Edit</button></a>

<?php
$i++;
}
?>

The problem is that it is only looping up to <p>Categories</p>. It does not loop the categories and edit part which is the $f3 variable and Edit button. The categories table is a separate table so I had set up a different query ($sql1) than the posts table. How can I include those parts into the loop so they show for each post? I tried moving the $i++ part to the bottom but that just showed me the same post infinitely. Thank you for your help.

4
  • What is the value of $num1? Echo it. Commented Oct 17, 2012 at 8:18
  • It is showing 1 because the first post in the loop has 1 category assigned to it. The second has 2 and that is what I want to show but isn't working. Commented Oct 17, 2012 at 8:27
  • your first loop uses $num, your second uses $num1. Did you actually echo $num1 before using it? If so, what is its value? Commented Oct 17, 2012 at 8:45
  • The value is 1. $num is the number of rows for the posts. $num1 is the number of rows of categories assigned to each post. Commented Oct 17, 2012 at 8:52

2 Answers 2

1

Embeded loops

<?php
  $i=0;
  while ($i < $num) {

    $f0=mysql_result($sql,$i,"id");
    $f1=mysql_result($sql,$i,"title");
    $f2=mysql_result($sql,$i,"post");
?>

    <p><?php echo $f1; ?></p>
    <p><?php echo $f2; ?></p>
    <p>Categories:</p>

<?php
    $j=0;
    while ($j < $num1) {
      $f3=mysql_result($sql1,$j,"category");
      echo $f3; 
      $j++;
}
?>

<a href="edit.php?id=<?php echo $f0; ?>"><button>Edit</button></a>

<?php
$i++;
}
?>
Sign up to request clarification or add additional context in comments.

9 Comments

That works but only if the first post has a category assigned to it or if $num1 = 1 or more. What if the first post has no category assigned to it and $num1 = 0? How can I make the second loop still show the edit button?
Thank you, that makes the edit button visible but it shows the same categories for all posts as the first post. It is not checking each post for their categories.
do your sql query before "while ($j < $num1) {" so you will query the db for each post
I have all the queries above all the code I wrote. Do you mean put it between $j=0; and while ($j < $num1) {? I tried that and still get the same result.
put the line starting with "$sql1 = " between $j=0; and while ($j < $num1) {
|
0

Udan's answer was correct but wasn't looping through my categories for each post for some reason. I decided to use a different loop and got it working. Posting here in case anyone else has the same problem.

echo "<h2>Posts</h2>";
$sql = mysql_query("SELECT id, title, post FROM posts") or die(mysql_error());
while ($test = mysql_fetch_array($sql)) {?>
<table><tr><td><input type="hidden" name="id" value="<?php echo $test['id']; ?>" /></td></tr>
<tr><td><?php echo $test['title']; ?></td></tr>
<tr><td><?php echo nl2br($test['post']); ?></td></tr></table>
<?php
echo "Categories:";
$sql1 = mysql_query("SELECT categories.* FROM categories INNER JOIN post_category ON categories.id=post_category.cat WHERE post_category.post='".$test['id']."'") or die(mysql_error());
while ($test1 = mysql_fetch_array($sql1)) {?>
<table><tr><td><?php echo $test1['category']; ?></td></tr></table>
<?php
}
?>
<a href="edit.php?id=<?php echo $test['id']; ?>"><button>Edit</button></a>
<?php
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.