Solved

display images in a table

Posted on 2010-08-25
1
315 Views
Last Modified: 2013-12-25
I've been working on this since Friday and it's now driving me nuts....hope someone can help  me on this.

I two table in mysql; products and images.

one product can have one or more images.

I can get all the product imformation and display the images of that product, but I need a way of stacking the images i.e.

pic1(main)
pic2:pic3 (thumbs)
pic4:pic5(thumbs)

see screenshot 1 show the image dispaly i'm getting at the moment..

code:

 while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){
                       
                       for($i=0; $i < count($row["image_name"]); $i++){

echo '                 <tr align="center" valign="middle" id="tableThumbnail">';
echo '                 <td id="tableThumbnail">';
echo '                      <a href="../admin/img.php?id=' . $product_id . '">';
                                    echo "<img src=\"../../uploads/".$row["image_name"]."\" class=\"thumbnail_Portrait\" />";
echo '                      </a>';
                            if ( (($i+1) % 2) == 0 ) echo $newrow="</tr><tr>";
echo '                 </td>';
echo '              </tr>';
                         }
                   }

screenshot 2 is what im hoping to achieve.
<?php
session_start();
if (!isset ($_SESSION['agent']) or ($_SESSION['agent'] != md5($_SERVER['HTTP_USER_AGENT']))) {
  require_once ('../php/login_functions.php');
  $url = absolute_url();
  header("Content-Type:
        {$info['mime']}\n");
    header("Content-Disposition: inline;
        filename=\"$name\"\n");
    header("Content-Length: $fs\n");
  exit ();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="stylesheet" href="../css/styleSheet.css" type="text/css" />
<link rel="stylesheet" href="../css/pictureBox.css" type="text/css" />

<script type="text/javascript" src="../js/functions.js"></script>

 <script type="text/javascript">
    function cancelAction()
  </script>
<body>
	
<?php
$page_title = 'Product Admin';
include ('../includes/header.html');

$filename = "../../uploiads";
$extension =".jpg";

// Check for a valid user ID, through GET or POST:
if ((isset ($_GET['id'])) && (is_numeric($_GET['id']))) {
  $id = $_GET['id'];
}
elseif ((isset ($_POST['id'])) && (is_numeric($_POST['id']))) {
  $id = $_POST['id'];
}
else {
  echo '<p class="error">This page has been accessed in a error!</p>';
  exit ();
}
require_once ('../../secure/connect.php');

// Check if the form has been submitted:
if (isset ($_POST['submit'])) {

// Validate the incoming data...
  $errors = array();
  if (empty ($_POST['product_name'])) {
    $errors[] = 'Please enter the item\'s name';
  }
  else {
    $pn = mysqli_real_escape_string($mysqli, trim($_POST['product_name']));
  }
  if (empty ($_POST['product_price'])) {
    $errors[] = 'Please enter price of item!';
  }
  else {
    $p = mysqli_real_escape_string($mysqli, (float) $_POST['product_price']);
  }
  $d = mysqli_real_escape_string($mysqli, trim($_POST['description']));
  $di = mysqli_real_escape_string($mysqli, trim($_POST['dimension']));

  if (empty ($errors)) {

// If everything's OK.
// Make the query:
    $q = "UPDATE products SET product_name='$pn', product_price='$p', description='$d', dimension='$di' WHERE products.product_id=$id LIMIT 1";
    $r = @ mysqli_query($mysqli, $q);
    if (mysqli_affected_rows($mysqli) == 1) {

// If it ran OK.
// Print a message:
      echo '<p>The product has been updated.</p></br>
         <a href="search_product.php">Return to Admin</a>';
    }
    else {

// If it did not run OK.
      echo '<p class="error">The product could not be edited due to a system error.</p>';

// Public message.
      echo '<p>' . mysqli_error($mysqli) . '<br />Query: ' . $q . '</p>';
    }
  }
  else {

// Report Errors
    echo '<p class="error">The following error(s) occurred:<br />';
    foreach ($errors as $msg) {

//Print each error
      echo " - $msg<br />\n";
    }
    echo '</p><p>Please try again.</p>';
  }

// End of if (empty($errors)) IF.
}

// End of submit conditional.

// Retrieve the products information:
$q = "SELECT * FROM `products`, `images` WHERE products.product_id=$id AND products.product_id=images.product_id ORDER BY print_name LIMIT 0, 30";

//Display results
$r = mysqli_query($mysqli, $q);

//echo "mysqli_num_rows: ".mysqli_num_rows($r);

if(mysqli_num_rows($r) >=0){
     // Show Results
     $row = mysqli_fetch_array($r, MYSQLI_ASSOC);
       $product_id=$row["product_id"];
       $id=$row["id"];
       $product_code=$row["product_code"];
       $product_name=$row["product_name"];
       $description=$row["description"];
       $price=$row["product_price"];
       $description=$row["description"];
       $dimension=$row["dimension"];
       $print_name=$row["print_name"];      
       $image_name=$row["image_name"];
   
debug($image);

// Create the form:
echo '<div id="container" class="bodytext">';
echo '<form action="edit_copy.php" method="post" >';
echo '   <div id="editInfo">';
echo '     <div>Product Code:     <b>' . $product_code . '</b> </div>';
echo '     <div>Product Name:    <input type="text" name="product_name" size="40" maxlength="50" value="' . $product_name . '" /></div>';

           //Image Table
echo '     <div id="picarea">';
echo '          <div align="left"> Images:</div>';
echo '             <table>';
                      // Main Product Image
echo '                <tr align="center" valign="middle">';
echo '                  <td id="tableMainImage" colspan="2" >';
echo '                        <a href="../admin/img.php?id=' . $product_id . '">';
                                 
                                  echo "<img src=\"../../uploads/".$image_name."\" class=\"imgmain_Portrait\" />";
echo '                   </a>';
echo '                  </td>';
echo '                </tr>';

                    //Thimbnail Images
                   while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){
                       
                       for($i=0; $i < count($row["image_name"]); $i++){

echo '                 <tr align="center" valign="middle" id="tableThumbnail">';
echo '                 <td id="tableThumbnail">';
echo '                      <a href="../admin/img.php?id=' . $product_id . '">';
                                    echo "<img src=\"../../uploads/".$row["image_name"]."\" class=\"thumbnail_Portrait\" />";
echo '                      </a>';
                            if ( (($i+1) % 2) == 0 ) echo $newrow="</tr><tr>";
echo '                 </td>';
echo '              </tr>';
                         }
                   }
                     
echo '                <tr>';
echo '                    <td  align="right">Product Price (&pound):<input type="text" name="product_price" size="6" maxlength="6" value="' . $price . '" /></td>';
echo '                </tr>';
echo '            </table>';
echo '        </div>';

echo '       <div style="width:300px; position:relative; float:right">Description:<br /><textarea  name="description" rows="23" cols="45" >' . $description . '</textarea></div>';

echo '       <div style="width:300px; position:relative; float:right">Dimensions:<br /><textarea name="dimension" rows="5" cols="45">' . $dimension . '</textarea></div>';

echo '       <div style="width:730px; position:relative; float:left" align="right">';
echo '        <input type="hidden" name="submit" value="TRUE" />';
echo '        <input type="hidden" name="id" value="' . $product_id . '" />';
echo '        <input name="cancel" type="button" id="cancel" value="Cancel" onClick="cancelAction();"/>';
echo '        <input type="submit" name="submit" value="Submit" />';
echo '       </div>';

echo '    </div>';
echo '</form>';
echo '</div>'; 
}
else {

// not a vaild user
  echo '<p class="error">This page has been accessed in an error.</p>';
}   
  mysqli_free_result ($r);
 mysqli_close($mysqli);

function debug($array)
 {
 echo "<pre>";
 //print_r($array);
 print_r($image);
 
 echo "</pre>";
 
 }
?>
</body>
</html>

Open in new window

screenshot.jpg
screenshot3.jpg
0
Comment
Question by:irwells
1 Comment
 
LVL 5

Accepted Solution

by:
wmadrid1 earned 500 total points
ID: 33527443
In the code below the for statement is commented
you already are showing in colspan 2 the first image

I simply bucle the while, trying always show two td columns if there is the pair
with
if($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{
//showing the second column
}


<?php
session_start();
if (!isset ($_SESSION['agent']) or ($_SESSION['agent'] != md5($_SERVER['HTTP_USER_AGENT']))) {
  require_once ('../php/login_functions.php');
  $url = absolute_url();
  header("Content-Type:
        {$info['mime']}\n");
    header("Content-Disposition: inline;
        filename=\"$name\"\n");
    header("Content-Length: $fs\n");
  exit ();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="stylesheet" href="../css/styleSheet.css" type="text/css" />
<link rel="stylesheet" href="../css/pictureBox.css" type="text/css" />

<script type="text/javascript" src="../js/functions.js"></script>

 <script type="text/javascript">
    function cancelAction()
  </script>
<body>
        
<?php
$page_title = 'Product Admin';
include ('../includes/header.html');

$filename = "../../uploiads";
$extension =".jpg";

// Check for a valid user ID, through GET or POST:
if ((isset ($_GET['id'])) && (is_numeric($_GET['id']))) {
  $id = $_GET['id'];
}
elseif ((isset ($_POST['id'])) && (is_numeric($_POST['id']))) {
  $id = $_POST['id'];
}
else {
  echo '<p class="error">This page has been accessed in a error!</p>';
  exit ();
}
require_once ('../../secure/connect.php');

// Check if the form has been submitted:
if (isset ($_POST['submit'])) {

// Validate the incoming data...
  $errors = array();
  if (empty ($_POST['product_name'])) {
    $errors[] = 'Please enter the item\'s name';
  }
  else {
    $pn = mysqli_real_escape_string($mysqli, trim($_POST['product_name']));
  }
  if (empty ($_POST['product_price'])) {
    $errors[] = 'Please enter price of item!';
  }
  else {
    $p = mysqli_real_escape_string($mysqli, (float) $_POST['product_price']);
  }
  $d = mysqli_real_escape_string($mysqli, trim($_POST['description']));
  $di = mysqli_real_escape_string($mysqli, trim($_POST['dimension']));

  if (empty ($errors)) {

// If everything's OK.
// Make the query:
    $q = "UPDATE products SET product_name='$pn', product_price='$p', description='$d', dimension='$di' WHERE products.product_id=$id LIMIT 1";
    $r = @ mysqli_query($mysqli, $q);
    if (mysqli_affected_rows($mysqli) == 1) {

// If it ran OK.
// Print a message:
      echo '<p>The product has been updated.</p></br>
         <a href="search_product.php">Return to Admin</a>';
    }
    else {

// If it did not run OK.
      echo '<p class="error">The product could not be edited due to a system error.</p>';

// Public message.
      echo '<p>' . mysqli_error($mysqli) . '<br />Query: ' . $q . '</p>';
    }
  }
  else {

// Report Errors
    echo '<p class="error">The following error(s) occurred:<br />';
    foreach ($errors as $msg) {

//Print each error
      echo " - $msg<br />\n";
    }
    echo '</p><p>Please try again.</p>';
  }

// End of if (empty($errors)) IF.
}

// End of submit conditional.

// Retrieve the products information:
$q = "SELECT * FROM `products`, `images` WHERE products.product_id=$id AND products.product_id=images.product_id ORDER BY print_name LIMIT 0, 30";

//Display results
$r = mysqli_query($mysqli, $q);

//echo "mysqli_num_rows: ".mysqli_num_rows($r);

if(mysqli_num_rows($r) >=0){
     // Show Results
     $row = mysqli_fetch_array($r, MYSQLI_ASSOC);
       $product_id=$row["product_id"];
       $id=$row["id"];
       $product_code=$row["product_code"];
       $product_name=$row["product_name"];
       $description=$row["description"];
       $price=$row["product_price"];
       $description=$row["description"];
       $dimension=$row["dimension"];
       $print_name=$row["print_name"];      
       $image_name=$row["image_name"];
   
debug($image);

// Create the form:
echo '<div id="container" class="bodytext">';
echo '<form action="edit_copy.php" method="post" >';
echo '   <div id="editInfo">';
echo '     <div>Product Code:     <b>' . $product_code . '</b> </div>';
echo '     <div>Product Name:    <input type="text" name="product_name" size="40" maxlength="50" value="' . $product_name . '" /></div>';

           //Image Table
echo '     <div id="picarea">';
echo '          <div align="left"> Images:</div>';
echo '             <table>';
                      // Main Product Image
echo '                <tr align="center" valign="middle">';
echo '                  <td id="tableMainImage" colspan="2" >';
echo '                        <a href="../admin/img.php?id=' . $product_id . '">';
                                 
                                  echo "<img src=\"../../uploads/".$image_name."\" class=\"imgmain_Portrait\" />";
echo '                   </a>';
echo '                  </td>';
echo '                </tr>';

                    //Thimbnail Images
                   while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){
                       
//                       for($i=0; $i < count($row["image_name"]); $i++){

echo '                 <tr align="center" valign="middle" id="tableThumbnail">';
echo '                 <td id="tableThumbnail">';
echo '                      <a href="../admin/img.php?id=' . $product_id . '">';
                                    echo "<img src=\"../../uploads/".$row["image_name"]."\" class=\"thumbnail_Portrait\" />";
echo '                      </a>';
echo '                 </td>';
	                   if($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
				{

echo '                 <td id="tableThumbnail">';
echo '                      <a href="../admin/img.php?id=' . $product_id . '">';
                                    echo "<img src=\"../../uploads/".$row["image_name"]."\" class=\"thumbnail_Portrait\" />";
echo '                      </a>';
echo '                 </td>';
				}
echo '              </tr>';
//                         }
                   }
                     
echo '                <tr>';
echo '                    <td  align="right">Product Price (&pound):<input type="text" name="product_price" size="6" maxlength="6" value="' . $price . '" /></td>';
echo '                </tr>';
echo '            </table>';
echo '        </div>';

echo '       <div style="width:300px; position:relative; float:right">Description:<br /><textarea  name="description" rows="23" cols="45" >' . $description . '</textarea></div>';

echo '       <div style="width:300px; position:relative; float:right">Dimensions:<br /><textarea name="dimension" rows="5" cols="45">' . $dimension . '</textarea></div>';

echo '       <div style="width:730px; position:relative; float:left" align="right">';
echo '        <input type="hidden" name="submit" value="TRUE" />';
echo '        <input type="hidden" name="id" value="' . $product_id . '" />';
echo '        <input name="cancel" type="button" id="cancel" value="Cancel" onClick="cancelAction();"/>';
echo '        <input type="submit" name="submit" value="Submit" />';
echo '       </div>';

echo '    </div>';
echo '</form>';
echo '</div>'; 
}
else {

// not a vaild user
  echo '<p class="error">This page has been accessed in an error.</p>';
}   
  mysqli_free_result ($r);
 mysqli_close($mysqli);

function debug($array)
 {
 echo "<pre>";
 //print_r($array);
 print_r($image);
 
 echo "</pre>";
 
 }
?>
</body>
</html>

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question