![]() |
Best Way To Loop In A Table?
Hi all,
I'm wondering what the best way to loop in a table is? I'm pulling data from a database and want it to display like this: ![]() The only way I know how to do it is by making separate looks for each column like so: PHP Code:
Thanks in advance, Steven |
Hi Steven,
Take a look at the array_chunk() function. Specificly, take a look at this comment - PHP: array_chunk - Manual - it gives an example on how to do exactly what you want to do :-)Alan |
Thanks again, Alan. I tried taking the code and modifying it a little, but I don't know much about arrays - I will read more in-depth soon.
The example: PHP Code:
$rows as $row not sure what that means, $row is specified anywhere.I tried: PHP Code:
$num_rock in there somewhere. Just not sure where. I think I need to study this more.Also tried: PHP Code:
$category_artist_rock |
1 Attachment(s)
Hi Steven,
Here's a little cut/paste example and the results it displays :-) PHP Code:
![]() This example uses an array called $items but changing it to your mysql data should be fairly easy. Changing $items block of code to something like the following should work:PHP Code:
cd_artist column but you can add more columns there as required.Alan |
Thanks for telling me about array_chunk, never had a use for it in that way :).
|
Same here, thanks Alan. :-)
|
All hail Alan ;-) I'll give that a shot later, thanks!
|
No probs, glad it helped you all :-)
Alan |
It sure did heh. I've noticed a long time ago that there are various functions with the array_ function but I've never tried them out, or looked them up to what they do exactly.
++ Hint ++ Alan -> Make an article...? :-P |
Quote:
Alan |
Well... it kind of works :-D
I'm using this code: PHP Code:
![]() But my table looks like this: ![]() It seems to be returning only the first artist with the category "rock", and it's showing it twice for some reason. |
Hi Steven,
Can you add: PHP Code:
Thanks, Alan |
oops, my fault - I had a blond moment :-) I'm so used to Zend_Db's fetchAll() method that I forgot that mysql_fetch_array() only returns the first result and that you have to loop through it to get them all :-(
Remove the mysql_fetch_array() line and replace it with this: PHP Code:
Alan |
I knew it was something to do with mysql_fetch_array and loops. I was messing around but couldn't get anything to work. Thank you very much for that, Alan.
|
Just an addition.
I wanted to loop horizontaly - and add empty colums where needed. so - Code:
1 2 3 4 So here is a simple code for that :) PHP Code:
PHP Code:
HTML Code:
<table width="300"> |
I apologise if this has already been said, but it looks like nobody ever answered this question from the OP:
Quote:
The line in question was foreach($rows as $row). Foreach is a very useful looping construct that only works with an array. The first variable is the name of the array and the second is the name that each element in that array will have. Let's say I have the following array storing information about my family: PHP Code:
PHP Code:
PHP Code:
Well, let's say that instead of just numeric indices, our family array has names linked to family position: PHP Code:
PHP Code:
|
Thanks a lot, Solistus. Great explanation!
|
| All times are GMT. The time now is 10:50 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0