www.webdeveloper.com
Results 1 to 3 of 3

Thread: Php/Mysql loop issue

  1. #1
    Join Date
    Mar 2010
    Posts
    60

    Php/Mysql loop issue

    Hi all,

    This should be a simple issue to fix, Im trying to pull all rows from a table which contain image addresses. These go into a js sort of banner.

    The issue im having is when I run the page only the last row of the db table is being called. Ive tried loops to no success and if I use my_sql_fetch_assoc only the first row is returned.

    Can someone show me where im going wrong? heres the code
    Code:
     <?php
        $sql="SELECT b.message, r.rightphoto, l.leftphoto FROM blog b, rightphoto r, leftphoto l";
    			$result=mysql_query($sql) or die (mysql_error());
    
    			while($r=mysql_fetch_array($result)){
    				$num=mysql_numrows($result);
    			
    			$message=$r["message"];
    			$rightphoto=$r["rightphoto"];
    			$leftphoto=$r["leftphoto"];
    			
    			
    			}
    			?>
    The problem im having is with $leftphoto, which is being used here
    Code:
    <?php 
    			
    			echo"<li> <img src='leftphoto/$leftphoto'></li>"; 
    			//$i++;
    			
    			?>
    
                
                </ul>
    I cant seem to get past this issue, having a total brain fart.

    Any help would be greatly appreciated!
    Thanks in advance!

  2. #2
    Join Date
    Apr 2006
    Posts
    59
    In your loop, you are overwriting the previous data.
    PHP Code:
    <?php
    $arrData 
    = array();
    $sql="SELECT b.message, r.rightphoto, l.leftphoto FROM blog b, rightphoto r, leftphoto l";
    $result=mysql_query($sql) or die (mysql_error());
    $i=0
    while($r=mysql_fetch_array($result)){
        
    $num=mysql_numrows($result);

        
    //Store in array
        
    $arrData[$i]["message"]=$r["message"];
        
    $arrData[$i]["rightphoto"]=$r["rightphoto"];
        
    $arrData[$i]["leftphoto"]=$r["leftphoto"];
                
        
    $i++;
    }

    //Print data
    foreach($arrData AS $arrDataRow) {
        echo
    "<li> <img src='leftphoto/".$arrDataRow["leftphoto"]."'></li>";
    }
    ?>

  3. #3
    Join Date
    Mar 2010
    Posts
    60
    Thanks man i owe ya one!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles