php loops

These are my sql queries:
--------------------------------
mysql_select_db($database_egweb, $egweb);
$query_egweb = "SELECT * FROM eg_images";
$egweb = mysql_query($query_egweb, $egweb) or die(mysql_error());
$row_egweb = mysql_fetch_assoc($egweb);
$totalRows_egweb = mysql_num_rows($egweb);
$totalRows_egwebb = mysql_num_rows($egweb);
---------------------------------

This is in the body tag:

<?php do { ?>
       
 <a class='highslide' href='<?php echo $row_egweb['eg_lge']; ?>' title="<?php echo $row_egweb['eg_title']; ?>"
 onclick="return hs.expand(this, miniGalleryOptions1)">
      <img src='<?php echo $row_egweb['eg_thumb']; ?>' alt=''/></a>
           
            <?php } while ($row_egweb = mysql_fetch_assoc($egweb)); ?>

What I need to do is to get th Do loop to start showing images from the table row with an ID of > 4. As it stands at the moment, the code is working but is duplicating the first image
Can anyone help please ?
doctorbillTechAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

StingRaYCommented:
Try this

<?php
// query things
mysql_select_db($database_egweb, $egweb);
$query_egweb = "SELECT * FROM eg_images";
$egweb = mysql_query($query_egweb, $egweb) or die(mysql_error());
$totalRows_egweb = mysql_num_rows($egweb);
$totalRows_egwebb = mysql_num_rows($egweb);
?>

// body
<?php
while ($row_egweb = mysql_fetch_assoc($egweb)) { ?>
        
 <a class='highslide' href='<?php echo $row_egweb['eg_lge']; ?>' title="<?php echo $row_egweb['eg_title']; ?>"
 onclick="return hs.expand(this, miniGalleryOptions1)">
      <img src='<?php echo $row_egweb['eg_thumb']; ?>' alt=''/></a>
            
<?php } ?>

Open in new window

0
Scott MadeiraCommented:
I'm not a fan of whiles and do loops.  Here is another approach where you get an array of rows instead of one row at a time and use a foreach loop to iterate through your results.

 
<?php
mysql_select_db($database_egweb, $egweb);
$query_egweb = "SELECT * FROM eg_images";
$egweb = mysql_query($query_egweb, $egweb) or die(mysql_error());
$row_data = mysql_fetch_array($egweb, MYSQL_ASSOC);
$totalRows_egweb = mysql_num_rows($egweb);
$totalRows_egwebb = mysql_num_rows($egweb);
?>

<?php

foreach ($row_data as $row_egweb){
?>	
	      
 	<a class='highslide' href='<?php echo $row_egweb['eg_lge']; ?>' title="<?php echo $row_egweb['eg_title']; ?>"
 	onclick="return hs.expand(this, miniGalleryOptions1)">
      <img src='<?php echo $row_egweb['eg_thumb']; ?>' alt=''/></a>
            
<?php } ?>

Open in new window

0
doctorbillTechAuthor Commented:
I think you have not read this line:

What I need to do is to get the Do loop to start showing images from the table row with an ID of > 4.
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

StingRaYCommented:
$query_egweb = "SELECT * FROM eg_images WHERE eg_id > 4";
0
EMB01Commented:
>>  What I need to do is to get the Do loop to start showing images from the table row with an ID of > 4.

Then you should change your query to this:

$query_egweb = "SELECT * FROM eg_images WHERE id > 4";
0
Ray PaseurCommented:
This kind of construct looks like you've been reading Dreamweaver in the hope of learning PHP from it.  That's not the right place to learn.  It's like looking at an apple pie to try to learn how to make pies.  The pie won't tell you and Dreamweaver won't either.  Instead get this excellent little book that explains how to do the sorts of things you're working on.  It will tell you the "why" that goes with the resulting code.
http://www.sitepoint.com/books/phpmysql4/

Here is the original code annotated with comments.  As you can see, it throws away the first row from the query results set.  You may not want that.  If this code is "duplicating the first image" you probably have some kind of pollution in the data base.  A copy of phpMyAdmin (free and open source) will help you see what is really in the data base.
http://www.phpmyadmin.net/home_page/index.php

As a general rule, you would want to remove SELECT * and just select the columns you want to use.  In addition to WHERE, you would want to use ORDER and LIMIT clauses to get predictable results from the data base.  So a reasonable query might look something like this.

SELECT eg_lge, eg_title, eg_thumb, id FROM eg_images WHERE id > 4 ORDER BY id LIMIT 10

You might also find that your code is easier to read and modify if you use PHP to create the HTML string and then echo the string, rather than jumping back and forth from PHP to HTML and back.  When you learn about the HEREDOC notation for string variables you will find that it is amazingly powerful.  Most of its power lies in the ability to make it easy to separate code from presentation logic.
http://php.net/manual/en/language.types.string.php
// CONSTRUCT A QUERY STRING
$query_egweb = "SELECT * FROM eg_images";

// RUN THE QUERY OR DIE
$egweb = mysql_query($query_egweb, $egweb) or die(mysql_error());

// FETCH THE FIRST ROW OF THE RESULTS SET, MOVING THE POINTER TO THE SECOND ROW
$row_egweb = mysql_fetch_assoc($egweb);

// GET THE NUMBER OF ROWS (TWICE?)
$totalRows_egweb = mysql_num_rows($egweb);
$totalRows_egwebb = mysql_num_rows($egweb);

// GO INTO PHP, CREATE A DO CONTROL STRUCTURE, THEN DROP OUT OF PHP
<?php do { ?>

// CREATE SOME HTML AND PHP WITH UNNECESSARILY HARD-TO-READ INTERMIXED CODE      
 <a class='highslide' href='<?php echo $row_egweb['eg_lge']; ?>' title="<?php echo $row_egweb['eg_title']; ?>"
 onclick="return hs.expand(this, miniGalleryOptions1)">
      <img src='<?php echo $row_egweb['eg_thumb']; ?>' alt=''/></a>

// END OF THE DO CONTROL STRUCTURE           
<?php } while ($row_egweb = mysql_fetch_assoc($egweb)); ?>

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
Scott MadeiraCommented:
Sorry, I was responding to your problem of your first line duplicating.  As others have said, put the ID criteria in your SQL statement.

If it isn't possible to modify the SQL then I assume you have an ID field in each row so you can check for the ID in your loop prior to outputting the image and the link:

 
<?php
mysql_select_db($database_egweb, $egweb);
$query_egweb = "SELECT * FROM eg_images";
$egweb = mysql_query($query_egweb, $egweb) or die(mysql_error());
$row_data = mysql_fetch_array($egweb, MYSQL_ASSOC);
$totalRows_egweb = mysql_num_rows($egweb);
$totalRows_egwebb = mysql_num_rows($egweb);
?>

<?php

foreach ($row_data as $row_egweb){

	if ($row_egweb['eg_lge'] > 4){
?>	
	      
 		<a class='highslide' href='<?php echo $row_egweb['eg_lge']; ?>' title="<?php echo $row_egweb['eg_title']; ?>"
 		onclick="return hs.expand(this, miniGalleryOptions1)"><img src='<?php echo $row_egweb['eg_thumb']; ?>' alt=''/></a>
            
<?php 
}	
} ?>

Open in new window

0
Ray PaseurCommented:
Here is how I might write it.  It's not tested, but hopefully it's close enough to illustrate the principles.  Two notes... 1. We do not need to count the number of rows with mysql_num_rows() - we know it is 10 or less.  2. We can use extract safely because we explicitly selected only certain columns.  With SELECT * you would have a situation where an addition to the data base table would inject an unwanted variable into the symbol table.  SELECT * is a time bomb best left defused.

Anyway, hope that helps, and good luck with your project, ~Ray
// START THE HTML STRING
$html = NULL;

// CONSTRUCT A QUERY STRING
$query_egweb = "SELECT eg_lge, eg_title, eg_thumb, id FROM eg_images WHERE id > 4 ORDER BY id LIMIT 10";

// RUN THE QUERY OR DIE, SHOWING BOTH THE FAILING QUERY AND THE ERROR
$egweb = mysql_query($query_egweb, $egweb) or die("FAIL $query_egweb<br/>" . mysql_error());

// WITH EACH ROW, CREATE A LINK AND IMAGE
while ($row = mysql_fetch_assoc($egweb))
{
    // DO NOT USE THIS WITH SELECT *
    extract($row);
        
    // CREATE A PART OF THE HTML
    $new = <<<HEREDOC
<a class="highslide" 
   href="$eg_lge" 
   title="$eg_title" 
   onclick="return hs.expand(this, miniGalleryOptions1)">
<img src="$eg_thumb" alt="" />
</a>
HEREDOC;

    // ADD THIS PART TO THE HTML STRING
    $html .= $new;   
}

// RENDER THE HTML
echo $html;

Open in new window

0
doctorbillTechAuthor Commented:
Comment to Ray:
I am very glad that someone with your experience recomends the sitepoint php book - I purchased this a few weeks ago but have not got round to reading it yet. I will do so
0
doctorbillTechAuthor Commented:
All solutions had their merits and , combined, solved my problem
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.