Re: HTML layout and arrays (WAS : Brain not working, help needed :-)
| From: | Neil Smith [MVP, Digital media] | Date: | Mon, 29 Aug 2005 12:52:34 +0000 |
| Subject: | Re: HTML layout and arrays (WAS : Brain not working, help needed :-) | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-40165@lists.php.net to get a copy of this message | ||
At 03:46 29/08/2005 +0000, you wrote:
Message-Id: <200508290346.j7T3k2b6016804@ms-smtp-02.tampabay.rr.com> From: "Chris Payne" <chris@artistwd.com> To: <php-db@lists.php.net> Date: Sun, 28 Aug 2005 23:45:59 -0700 MIME-Version: 1.0 Content-Type: multipart/alternative;The variable type you need is called an array, it has numerical indexes : In PHP you can just omit the index and it'll be automatically auto-incremented from zero, so there's no need to maintain a counter : $videos=array(); while ($row = mysql_fetch_array($sql_result)) { $videos[]["id"] = $row["id"]; $videos[]["video_link"] = $row["video_link"]; $videos[]["video_thumbnail"] = $row["video_thumbname"]; $videos[]["video_title"] = $row["video_title"]; $videos[]["video_description"] = $row["video_description"]; }; print_r($videos); From that you can see a 6 item array, (0-5 number-indexed ), and each item is itself an array, with elements indexed by name not number - "id", "video_link" etc etc.boundary="----=_NextPart_000_0000_01C5AC2A.A4CF7370"Subject: Brain not working, help needed :-) Hi there everyone, have the following code: while ($row = mysql_fetch_array($sql_result)) { $id = $row["id"]; $video_link = $row["video_link"]; $video_thumbnail = $row["video_thumbname"]; $video_title = $row["video_title"]; $video_description = $row["video_description"]; }; Now I'll be shifting through my MySQL array 6 items at a time, what I'd LIKE to be able to do is store each item in a number string on each pass. In other words, the first loop would see $id being called something like $id1, the second pass in the loop it would be $id2 and so on.
I THOUGHT I could do this by putting a $count ++ in the loop and then doing something like $id$count or something like $id.$count but neither worked,You can in PHP using "variable variables" but it gets very complicated and hard to debug. Stick with arrays so you can use stuff like for ($i=0; $i<count($videos); $i++) {
print_r($videos[$i]);} or foreach ($videos as $video_index=>$video) {
print("Index : ".$video_index."<br />Contents : ");
print_r($video);
}
I have 6 items on the page so I can hardcode <?=$id1?> for item 1 andInstead, use <?=$videos[0]["video_description"]?> and so on. Cheers - Neil