I'm trying to output the results of an array to a table. To be more specific, I'm trying to output pictures ... let's say 5 pics per row for example. Below each pic I'd like to display a few options like delete, etc.

I'm not looking for the answer, just to be pointed in the right direction of where to start so I can research and experiment a little. Where should I start, a for loop maybe ? I'm not sure what I should be looking up to get this accomplished.

Any advice is appreciated.

Recommended Answers

All 9 Replies

You could use a for or while loop to do this.

For Example:

$pictures = array('beach.png' => 'Image of the Beach', 'mountain.png' => 'Picture of a mountain');

foreach($pictures as $image => $title){
   echo '<img src="'.$image.'" /><br/>'.$title.'<br/>Edit | Delete<br/>';
}

Hope this helps

Thanks PHPER ... I'll start there. Up to this point I've only been using while loops and I can't accomplish what I want to with a while loop. I thought this would be no problem but it's proving to be a bit more than I bargained on for a beginner.

you will need to loops, one loop maintaining track of the rows, and the other maintaining track of the columns.
e.g.

<?php

$count = 0;
for($i=0; $i<3; $i++){ //num of rows
  for($j=0; $j<5; $j++) //num of cols
     echo "Number ".++$count."  ";
  echo "<br>";
}

?>

I never tried anything like this, I'll play around a little and see what I can come up with. It's a bit tougher than I thought it would be. Thank you for the pointers.

Wilch, I've been using your code to experiment and see if I can display pictures the way I want. I'm not getting quite the result I'm looking for and not quite sure what to change to accomplish my goal. I've been reading up on for loops and the examples seem fairly cut and dry (for the most part) but I'm really confusing myself when I try to plug a photo into the equation.

Here is what I did to butcher your code so far (sorry for that)

$sql="SELECT pic FROM user_profile_pics WHERE user_id='$uid' AND pic>'1' order by def desc";
$res=mysql_query($sql);
 while($r=mysql_fetch_array($res)){ 
 
$photo="<img src=\"..xpics/profiles/small/".$r['pic']."\">";}
for($i=0; $i<3; $i++){ //num of rows
  for($j=0; $j<5; $j++) //num of cols
     echo "".++$photo."  ";
  echo "<br>";
}

I changed up the code lots of diff ways and got some very interesting results to say the least. Seems like I'm closer than I was, but still not understanding it completely.

If you put the pictures into an list <li> it would be easier to keep updated. You could style them to show 5 pictures per row. Then later, if you need 3 pictures per row, you can just adjust your css, rather than your entire table structure.

But if you want to do tables, heres some code that may get you started:

//$img[row][column]

$img[0][0] = "img1.jpg";
$img[0][1] = "img2.jpg";
$img[0][2] = "img3.jpg";
$img[0][3] = "img4.jpg";
$img[0][4] = "img5.jpg";

$img[1][0] = "img6.jpg";
$img[1][1] = "img7.jpg";
$img[1][2] = "img8.jpg";
$img[1][3] = "img9.jpg";
$img[1][4] = "img0.jpg";
echo "<table>";
foreach($img as $row){
	echo "<tr>\n";
	foreach($row as $col){
		echo "<td>";
		echo $col;
		echo "</td>\n";
	}
	echo "</tr>\n";
}
echo "</table>";

I like the foreach loop, I was playing around with something similar myself (although my understanding of them is vague). My question is that my list of images is coming from a search result, I'm not using a predefined list. How can i populate that list from a result set? I'm playing around with it now.

Thanks for the help guys, I got this one worked out. I used a sample I found on the net and I really hate to do that because I feel that cut and paste really isn't teaching you anything. Anyhow, it is what it is. Thanks again for the help.

Hello CFrog:

Would you mind posting you solution? It would be very helpful. Thanks.

Thanks for the help guys, I got this one worked out. I used a sample I found on the net and I really hate to do that because I feel that cut and paste really isn't teaching you anything. Anyhow, it is what it is. Thanks again for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.