By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,275 Members | 947 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,275 IT Pros & Developers. It's quick & easy.

displaying a varying number of images.. in tables?!

P: n/a
Hey all.

I'm building a news system. I have a page to display all posts made to
category=$id, simple enough, now I wanna make an index page that looks
up all the categories from the news_topics table, displays their name,
a link to their page, and the image assigned to that category.

Here's the catch: I wanna put this in a table as the images are only a
few hundred pixels wide, so a flat list of each image would be boring
and inefficient. The thing is, we may add/remove news categories over
time, so how can I deal with this?

Would it really be easier to manually make this in HTML and change it
as/when we make modifications to the topics table?

-Matt

Jul 28 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Don't do this manually. You just need to have some code that will take
all of the images that need to be displayed in an array (say $images is
an array that contains the HTML code for all of the images) and have a
loop that goes through $images putting it in a table. It could have a
variable that keeps track of what column its on, and when it reaches a
certain number, go to the next row and reset the column counter
variable. When it reaches the end of $images, it just writes a few
"<td></td>" 's to fill the rest of that row, then closes the table.

Jul 29 '05 #2

P: n/a
I assume you've already connected to the mysql server, that you have
chosen database and that the connection is open... Then this should be
somehow what like what you need:

//Do query

$query = "SELECT * FROM `news_categories`";

$resultat = mysql_query($query);

//Start loop

while ($row= @mysql_fetch_array($resultat)) {

//Fetch info from the row
$cat_name = $row["cat_name"]; //fetch the name of the category
$cat_link = $row["cat_link"]; //fetch the link to the category
$cat_image = $row["cat_image"]; //fetch the image assigned to the
category

//Make the HTML

echo("<tr><td>$cat_name</td><td><a
href=\"$cat_link\">$cat_link</a></td><td><img src=\"$cat_img\"
alt=\"cat_name\"></td></tr>

$count++ ;
}

I have not tested it, but it should work...

Jul 29 '05 #3

P: n/a
doffer:

thanks for the code, but all it does it display a table of images one
by one in rows.. not what I need. I know theoretically what I need to
do (count the number of rows returned, and every three rows or so, echo
a "</td><td>") but I don't know how to actually code it.

Jul 29 '05 #4

P: n/a
I noticed that Message-ID:
<11**********************@g47g2000cwa.googlegroups .com> from
gu************@gmail.com contained the following:
thanks for the code, but all it does it display a table of images one
by one in rows.. not what I need. I know theoretically what I need to
do (count the number of rows returned, and every three rows or so, echo
a "</td><td>") but I don't know how to actually code it.


Probably not the most elegant way but...

$result=mysql_query($sql);

$row[0]="";
$columns=3;
$i=0;
$j=0;
print "<table>\n";
while($myrow=mysql_fetch_array($result)){

if($i==0 || $i%$columns!==0){
$row[$j].=" <td>".$myrow['Make']."</td>\n";
$i++;
}
else{
$j++;
$row[$j]="";
$row[$j].=" <td>".$myrow['Make']."$j</td>\n";
$i++;
}
}
while($i%$columns!==0){
$row[$j].=" <td>&nbsp;</td>\n";
$i++;
}
foreach($row as $value){
print " <tr>\n$value </tr>\n";
}
print"</table>";

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 29 '05 #5

P: n/a
I noticed that Message-ID: <r5********************************@4ax.com>
from Geoff Berrow contained the following:
Probably not the most elegant way but...

Forgot to get rid of the debugging

$result=mysql_query($sql);

$row[0]="";
$columns=3;
$i=0;
$j=0;
print "<table>\n";
while($myrow=mysql_fetch_array($result)){

if($i==0 || $i%$columns!==0){
$row[$j].=" <td>".$myrow['myfield']."</td>\n";
$i++;
}
else{
$j++;
$row[$j]="";
$row[$j].=" <td>".$myrow['myfield']."</td>\n";
$i++;
}
}
while($i%$columns!==0){
$row[$j].=" <td>&nbsp;</td>\n";
$i++;
}
foreach($row as $value){
print " <tr>\n$value </tr>\n";
}
print"</table>";
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 29 '05 #6

P: n/a
Thanks Geoff! After a few tweaks to display the images that worked
perfectly:
http://www.scenepointblank.com/matt/dev/news/

I need to credit you somewhere on this site when it's all done for all
the support, haha..

Jul 29 '05 #7

P: n/a
I noticed that Message-ID:
<11**********************@g49g2000cwa.googlegroups .com> from
gu************@gmail.com contained the following:
Thanks Geoff! After a few tweaks to display the images that worked
perfectly:
http://www.scenepointblank.com/matt/dev/news/

I need to credit you somewhere on this site when it's all done for all
the support, haha..


That would be nice :)

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 29 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.