Jump to content


Echo images into rows and columns


8 replies to this topic

#1 Waffles

    Young Padawan

  • Members
  • Pip
  • 8 posts

Posted 28 November 2005 - 03:56 PM

Evening

Right I've coded up a little affiliates system for users on my site, that enables them to add the their website URL and a direct link to an image. It then spits it back out as an affiliate with the URL attached on my Affiliates page.

Below is my code for displaying the banners.

As of now, each banner is echoed into its own table, and it goes one affiliate to a row..if you can visualise that?

I want ideally, 5 affiliates to a row, then the row is culled, and a new one is started, and so forth until all banners are displayed.

The code:

<?php
$query="SELECT * from affiliates ";
$result=mysql_query($query) or die ("Could not execute query: $q.". mysql_error());

while($row=mysql_fetch_array($result))
{
extract($row); // extract row values

// exit php, html for a table the correct size of a banner, echo the banner and the matching ID. this needs changing, i believe.
?>
<table width="88" height="31" border="0" cellpadding="0" cellspacing="2">
<tr>
<td align="center">
<?php echo "<a href=\"$url\" target=\"_blank\"><img src=\"$banner\" width=\"88\" height=\"31\" border=\"0\">";?></td>
</tr>
</table>
<?php
} // back in to php and end  while statement 
?>

My only suggestion on how to accomplish this is to add a counter inside the loop, so when it equals 5, to cull the row and start a new one.

I have no idea where to start modifying that code to do this, any help greatly appreciated.

http://wafflesweb.co...tion=affiliates is the link, if you need to clarify what I mean...

Thanks

Edited by Waffles, 28 November 2005 - 03:58 PM.


#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 28 November 2005 - 04:21 PM

You mean this?
http://www.pixel2lif...showtopic=15327

#3 Waffles

    Young Padawan

  • Members
  • Pip
  • 8 posts

Posted 28 November 2005 - 05:06 PM

tried it, didn't work...unless its just me, probably.

I think that's far too much, for what I need anyway.

#4 JoeVincetti

    Young Padawan

  • Members
  • Pip
  • 82 posts

Posted 28 November 2005 - 11:13 PM

A way of shorting images into [x] images per row would be:

//for formatting
echo "<table>";
echo " <tr>";

//connect to db, change to yours
mysql_connect(localhost,$dbuser,$dbpassword);
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query("SELECT * FROM photos"); 
$num=mysql_numrows($result);

$i=0;
while ($i < $num) {

	//customize this to the variables you want to grab
	$photos_id=mysql_result($result,$i,"id");
	$photos_title=mysql_result($result,$i,"title");
	$photos_summary=mysql_result($result,$i,"summary");
	$photos_url=mysql_result($result,$i,"url");
	$photos_date=mysql_result($result,$i,"date");

		 //customize to display image	
	 echo "<td>$photos_id <td>";

	// [x] photos per row, change this to the number of photos/images you want per row
	 $images_per_row = 5;

	//Here's the key part, this adds a new row after the [x]result/image from the database is echo-d
	if ($i % $images_per_row == 0) {
		echo "<tr></tr>";
	} 

	$i++;
}
echo "</table>";
mysql_close();
?>

Edited by JoeVincetti, 28 November 2005 - 11:19 PM.


#5 Waffles

    Young Padawan

  • Members
  • Pip
  • 8 posts

Posted 29 November 2005 - 12:45 PM

excellent..thank you.

#6 dxw

    Young Padawan

  • Members
  • Pip
  • 8 posts

Posted 10 May 2010 - 07:48 PM

Hi everyone this is a great help as it is the closest I have found to what I need :) , but cannot get over a few minor points that are really begining to bug me, hope someone can help:

Here is my code

<?php
 
 echo"<table width='100%'>";
 echo"<tr>";
 
 $menscategory_result = mysql_query("SELECT category_name FROM {$clientgallerycategorypath} WHERE category_name LIKE '%".$mens."%' ");
 $menscategory_num=mysql_numrows($menscategory_result);
 
 $menscategory_i=0;
 while ($menscategory_i < $menscategory_num) {
 
	 //customize this to the variables you want to grab
	 $menscategory_name=mysql_result($menscategory_result,$menscategory_i,"category_name");
 
		  //customize to display image	
	  echo "<td>$menscategory_name <td>";
 
	 // [x] photos per row, change this to the number of photos/images you want per row
	  $images_per_row = 4;
 
	 //Here's the key part, this adds a new row after the [x]result/image from the database is echo-d
	 if ($menscategory_i % $images_per_row == 0) {
		 echo "<tr></tr>";
	 }
 
	 $menscategory_i++;
 }
 echo "</table>";
 
 ?>

Now this works fine apart from when the code is outputted it shows one entry then starts a new line and shows 4 then it starts a new line again and shows on more ( I have six entrys )

The output it shows gives is:

<table width='100%'><tr><td>Mens tester 1 <td><tr></tr><td>Mens tester 2 <td><td>Mens Shirts <td><td>Menswear <td><td>Mens Footwear <td><tr></tr><td>Mens Leather <td><td>Mens Leisurewear <td></table>


and I cannot figure it out i know its to do with the first <tr> but I need to have this done for a client tomorrow morning and have been stuck on it for hours now please help :)

Edited by dxw, 10 May 2010 - 07:55 PM.


#7 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 10 May 2010 - 10:46 PM

The following is neither tested nor my best work. But it should do the job.
<?php
echo "<table width='100%'>";

$menscategory_result = mysql_query("SELECT category_name FROM {$clientgallerycategorypath} WHERE category_name LIKE '%".$mens."%' ");

// [x] photos per row, change this to the number of photos/images you want per row
$images_per_row = 4;

for($x = 0; $r = mysql_fetch_array($menscategory_result); $x++){
	if($x % $images_per_row == 0){
		echo "<tr>";
	}
	
	//customize this to the variables you want to grab
	$menscategory_name = $r["category_name"];
	
	//customize to display image
	echo "<td>$menscategory_name <td>";
	
	//Here's the key part, this adds a new row after the [x]result/image from the database is echo'd
	if ($x % $images_per_row == ($images_per_row)-1) {
		echo "</tr>";
	}
}

if($x != mysql_num_rows($menscategory_result)){
	echo "</tr>";
}

echo "</table>";
?>

Edited by rc69, 10 May 2010 - 10:47 PM.


#8 dxw

    Young Padawan

  • Members
  • Pip
  • 8 posts

Posted 11 May 2010 - 01:44 AM

Thanks so much for your help that has solved it straight away :)

#9 aprillove20

    Young Padawan

  • Members
  • Pip
  • 140 posts

Posted 24 August 2010 - 02:27 PM

Well, this is a great help and good resource. Nicely presented information in this thread.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users