integrating PHP with HTML page to execute SQL Query

Discussing Html . Code , Software , other

Moderators: macek, egami, gesf

Post Reply
User avatar
L33R
New php-forum User
New php-forum User
Posts: 144
Joined: Fri Dec 30, 2011 4:27 am
Location: Liverpool

Re: integrating PHP with HTML page to execute SQL Query

Post by L33R » Mon Apr 23, 2012 4:53 am

Ive had a quick look. I havent looked at every little part so heres what is wrong with what I have looked at. There may be more wrong with it:

This while loop below...I guess that if there are 50 images you want to view them all?
The while loop below just reads through all of the images and overwrites the variables each time .

You need to extend the while loop to include the image divs.

You also need to specify the file name to the src tag of img : <img src="data/images" <img src="$filename"

You also have floating text. you should be using <p> <span> tags etc...

Your code:

Code: Select all

                       <?php
      $link = mysql_connect("server", "user","pwd");
mysql_select_db("db");
$sql=("SELECT * FROM images WHERE user_id='".$user_id."'");
$result=mysql_query($sql);
while($r=mysql_fetch_array($result))
{   
  
   $user_id=$r["user_id"];
   $date_image_taken=$r["date_image_taken"];
   $image_taken_by=$r["image_taken_by"];
   $file_name=$r["file_name"];
   $notes=$r["notes"];

 
}
?>
                         
                        
                        <div class="category" data-title="Images" data-thumb="data/images">
                    
                            <ul>
                        
                                <li>
                                     <img src="data/images" alt="data/images/images" width="80" height="53" title="<?php echo $file_name; ?>" data-description= USER ID:   "<?php echo $user_id; ?>"<br/><br/>   
Date Image Taken:   "<?php echo $date_image_taken; ?>"<br/><br/>
Image Taken By:      "<?php echo $image_taken_by; ?>"<br/><br/>
Notes:            "<?php echo $notes; ?>""/>
                                </li>
                                                                                       
                         </ul> 
                    
                    </div>
 




After my changes:

(there is still a lot of room for improving this code but here is an example of it working)

Code: Select all


<?php
$link = mysql_connect("server", "user","pwd");
mysql_select_db("db");
$sql=("SELECT * FROM images WHERE user_id='".$user_id."'");
$result=mysql_query($sql);

$image_output ="<ul>"; //open the unordered list outside of the loop 

while($r=mysql_fetch_array($result))
{   
  
   $user_id=$r["user_id"];
   $date_image_taken=$r["date_image_taken"];
   $image_taken_by=$r["image_taken_by"];
   $file_name=$r["file_name"];
   $notes=$r["notes"];

     // build the image list here 
    $image_output  .= sprintf("
                                <li>
                        <img src='%s' alt=\"\" width=\"80\" height=\"53\" title='%s' data-description= USER ID:   %s<br/><br/>   
						Date Image Taken:   %s<br/><br/>
						Image Taken By:      %s<br/><br/>
						Notes:            %s />
                                </li>",$filename,$filename,$user_id,$date_image_taken,$image_taken_by,$notes);
}
$image_output .= "</ul>";

?>

Code: Select all


                        <div class="category" data-title="Images" data-thumb="data/images">
                    
                            <?php echo $image_output; ?>
                    </div>


Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest