Re: HTML layout and arrays (WAS : Brain not working, help needed :-)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 03:46 29/08/2005 +0000, you wrote:

Message-Id: <200508290346.j7T3k2b6016804@xxxxxxxxxxxxxxxxxxxxxxxxxx>
From: "Chris Payne" <chris@xxxxxxxxxxxx>
To: <php-db@xxxxxxxxxxxxx>
Date: Sun, 28 Aug 2005 23:45:59 -0700
MIME-Version: 1.0
Content-Type: multipart/alternative;
        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.

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.

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 and

Instead, use <?=$videos[0]["video_description"]?> and so on.


Cheers - Neil
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux