2

So basicly what I'm trying to accomplish is that foreach row in mysql query it prints out the html with the data from that row. Here's what I have, it keeps giving me an error on my foreach.

<?php

$shots = mysql_query("SELECT * FROM shots") or die(mysql_error());

while($row=mysql_fetch_array($shots))
$data[]=$row;


foreach($shots as $data)

  if (!empty($data)){

  $id = $data["id"];
  $shotby = $data["shot"];
  $passby = $data["pass"];
  $time = $data["time"];
?>


<div class="feedbody">

    <div class="title"><?php echo $shotby; ?></div>
    <div class="feed-data">: gets a pass from <span><?php echo $passby; ?</span> and he takes a shot!</div>
    <img class="dot" src="images/dot.png" />

</div>
<?php
}

}
?>

Or something like that. Can anybody help point me in the right direction. I've been trying to find the answer.

EDIT: adding the error as requested.

Warning: Invalid argument supplied for foreach() in /home/content/93/7527593/html/fusionboard/includes/feed.php on line 7
2
  • 2
    you forgot something. The error. Commented Jan 16, 2012 at 18:40
  • Also, I know it's a coding style thing, but please use curly braces, even on one-line bodies like your while loop and for loop. Makes everything much more readable and one day not using them will come back to bite you with a hard to track down bug. Commented Jan 16, 2012 at 18:55

5 Answers 5

11

First, if you want to access the data by name (instead of index), you need to include MYSQL_ASSOC as a second parameter to mysql_fetch_array, or use mysql_fetch_assoc.

Not really sure why you were copying the MySQL results to a second array just to loop through that later - you can loop through the results directly:

<?php
$shots = mysql_query("SELECT * FROM shots") or die(mysql_error());
while($row = mysql_fetch_assoc($shots)) { ?>
<div class="feedbody">
    <div class="title"><?php echo $row["shot"]; ?></div>
    <div class="feed-data">: gets a pass from <span><?php echo $row["pass"]; ?></span> and he takes a shot!</div>
    <img class="dot" src="images/dot.png" />
</div>
<?php } ?>

Update after you posted the error message: the error from your original code was that you first went through and copied each result row into $data, but then in your foreach you tried to loop on $shots (again) and have it call each item $data.

What you probably wanted to do was have foreach ($data as $item) and then copy the properties from $item.

Sign up to request clarification or add additional context in comments.

4 Comments

You could just make it while($row = mysql_fetch_assoc($shots)) for a associative array. :) - upvoted though
@biotox thanks, updated answer to reflect that (been a while since I've used native PHP mysql calls, ha).
It's cause I'm not really sure what I'm doing half the time. Thanks a lot for the help.
Agreed. mysql_fetch_assoc() doesn't require anything more.
2

Something like this?

<?php
$shots = mysql_query("SELECT * FROM shots") or die(mysql_error());

while($row=mysql_fetch_assoc($shots))
{
  $id = $row["id"];
  $shotby = $row["shot"];
  $passby = $row["pass"];
  $time = $row["time"];
?>

<div class="feedbody">

    <div class="title"><?php echo $shotby; ?></div>
    <div class="feed-data">: gets a pass from <span><?php echo $passby; ?</span> and he takes a shot!</div>
    <img class="dot" src="images/dot.png" />

</div>
<?php
}
?>

Comments

1

Perhaps you need another closing brace? (Another "}" at the end, I mean).

Comments

0

You saved your data in the $data variable, but your foreach uses the $shots variable.

Just change it to foreach($data as $something) and $something["id"] (for example) to retrieve a value

Comments

-2

you are using one variable instead of another.
and many useless code.

while youneed only

<?php foreach($data as $row) { ?> 
<div class="feedbody"> 
    <div class="title"><?php echo $row['shotby'] ?></div> 
    <div class="feed-data">: 
        gets a pass from <span><?php echo $row['passby'] ?</span> and he takes a shot!
    </div> 
    <img class="dot" src="images/dot.png" /> 
</div> 
<?php } ?>

1 Comment

you intend to loop through this for every column of every row?

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.