PHP Coding for image display

OK, I have a database with images stored in it.  Right now it has 13 images.  That database number will grow/shrink over time.

What I want to do is to display them in a row of 5, row of 4, row of 5, row of 4,.... pattern.  I know the last row will probably not meet the criteria, but that is OK.

I am having a brain freeze as to how to do this dynamically in PHP.  I know I need some loops, just cannot figure it out so.... help please

dickep
dickepAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JF0Commented:
I don't write a lot of php so I don't know how efficient this is BUT IT WORKS! :)
<?php
$switch = 0;
for ($i=1; $i<=50; $i++)
	{
		if ($switch==0)
			{
				for ($iy=1; $iy<=5; $iy++)
					{
						echo "The number is " . $iy . "<br />";
					}
				$switch=1;
			}
		else
			{
				for ($iy=1; $iy<=4; $iy++)
					{
						echo "The number is " . $iy . "<br />";
					}
				$switch=0;
			}
	}
?>

Open in new window

0
dickepAuthor Commented:
JF0,

I see your logic, but it is not quite what I am trying to do.  What would happen if I have say 11 images?  How would this terminate without an error?

So, I am still in a quandry as to what to do and how to do it.

0
JF0Commented:
The number of images does not matter. Simply replace the hard coded values with variables based on your database. In this example I used 50 but you can change that to 11 or 42 or over 9000 if you like.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Lukasz ChmielewskiCommented:
By that loop you mean to display it like that:
o o o o o
o o o o
o o o o o
o o o o
etc. ?
0
dickepAuthor Commented:
Roads Roads - yes that is the pattern, well, actually

  X   X   X   X
X   X   X   X  X
   X  X    X  X
 X  X  X    X   X
0
JF0Commented:
dickep do you see that there is no chance for my code to terminate with errors? It very simply will go through any number of items and display them in groups of 5 and 4 repeatedly.
0
dickepAuthor Commented:
JFO

As written, it will complete OK.  But what if you have only 48 images?  Then the internal loop (never sure which one) would be trying to get something that is not there.  See my point?

Just need a better checking mechanism.  Am thinking of while loops with a master counter in each sub-loop.

Testing that theory now.
0
JF0Commented:
I misunderstood the results of my original code, sorry about that! Try this new one out.
<?php

$switch = 1;
$ix=1;

for ($i=1; $i<=48; $i++)
	{
		if ($switch==1)
			{
				if ($ix<=4)
					{
						echo "The number is " . $i . "<br />";
						$ix++;
					}
				
				else
					{
						$switch=0;
						$ix=1;
						$i=$i-1;
						echo "<br />";
						echo "<br />";
					}
			}
		else
			{
				if ($ix<=5)
					{
						echo "The number is " . $i . "<br />";
						$ix++;
					}
				else
					{
						$switch=1;
						$ix=1;
						echo "<br />";
						echo "<br />";
						$i=$i-1;
					}
			}
	}
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dickepAuthor Commented:
JFO,

With your example, I did the following to make it work for me (this is a snippet of my PHP code).  

$image_count = 1;  // counter to keep track of images displayed
                     $switch = 0;
                      while ($image_count <= $num_rows) // $num_rows is the total number of images in my database
                      	{
                      	   $r = 1;
                      	   if ($switch == 0) 
                           {
                              $q = 4;
                              $switch = 1;
                           }
                           else
                           {
                              $q = 5;
                              $switch = 0;
                           }
                      	   while (($r <= $q) && ($image_count <= $num_rows))
                      	   {
                              $row = mysql_fetch_array($images, MYSQL_NUM);
                              // $row[0] = Picture ID (PixId)
                              // $row[1] = thumbnail file path and image name
                              // $row[2] = large image file path and image name
                              // $row[3] = image caption
                              // $row[4] = date of picture (currently unused)
                              echo .$row[2].'" class="link2" rel="gallery" title="'.$row[3].'">';
                              echo .$row[1].'" width="130" height="100"></a>';
                              $image_count++ ;
                              $r++ ;
                            }
                            
                            
                            echo '<br>';
                
                              
                       }       

Open in new window


So, I am going to award you some points - just not all.

Thanks for all your help and guidance.  I sometimes need someone to point me back on the path from the trees.
0
dickepAuthor Commented:
Only awarding half since JF0 pointed me, rightly so, to the "final solution" so to speak.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.