Jump to content

Insert ID to hyperlink with PHP


SupremeBeing

Recommended Posts

Hello Everyone.

 

Im new here, but hopefully you can help me :)

 

I have designed a CMS using PHP and MySQL. Its fairly basic, I'll talk you thorugh the stages:

 

1.) The user inputs their product information into a form and the script uploads it to the database giving it a unique ID.

2.) The user is then asked to upload a big image for that product, the image is added to a folder on the server.

3.) Now the user must choose a thumbnail and it uploads in the same manner.

 

My problem is linking the two together...

So far I have this:

 

<?php 

$images = "products/"; 
$big    = "big/"; 
$cols   = 2;  

if ($handle = opendir($images)) { 
   while (false !== ($file = readdir($handle))) { 
       if ($file != "." && $file != ".." && $file != rtrim($big,"/")) { 
           $files[] = $file; 
       } 
   } 
   closedir($handle); 
} 

$colCtr = 0; 

echo '<table width="212" cellpadding="2"><tr>'; 

foreach($files as $file) 
{ 
  if($colCtr %$cols == 0) 
    echo '</tr><tr><td colspan="2"></td></tr><tr>'; 
  echo '<td wdith=50% align="center"><a href="' . $images . $big . $file . '"><img border="0" class="gradualfader" src="' . $images . $file . '" /></a></td>'; 
  $colCtr++; 
} 

echo '</table>' . "\r\n"; 

?>

 

This displays a lovely list of thumbnails, which when clicked opens the larger image. What i would like to do is insert a "ID" for each image so the product data can be grabbed from the database ?

 

I think i've explained this well enough? reply if not and illl try and add more detail.

 

To see it in action you can go to http://design.surreylabs.co.uk/parliament/2/products.php

 

Thanks in Advance!

 

Link to comment
Share on other sites

It might also be worth mentioning, that when images are uploaded they are renamed to "1.jpg", "2.jpg" etc so that they correspond with the information added in the database.

 

Eg: The first record in the database has an ID of 1, so does the image and the thumb.

 

All i need is to insert that id into the hyperlink, so instead of opening the bigger image it reloads the page with products.php?id=1 and thus grabs the data from the database.

 

I think that makes more sense. (Sorry for the double post).

Link to comment
Share on other sites

Can't you select all the records from the database?

 

Or you could remove the extension and use just the number, either by using pathinfo (the constant to use requires PHP 5.2) or you could manually remove it.

 

Example:

$file = '12.jpg';
$id = array_shift(explode('.', $file));

Then you just need to create the link.

foreach($files as $file) 
{ 
  if($colCtr %$cols == 0) 
    $id = array_shift(explode('.', $file));
    echo '</tr><tr><td colspan="2"></td></tr><tr>'; 
  echo '<td wdith=50% align="center"><a href="products.php?id=' . $id . '"><img border="0" class="gradualfader" src="' . $images . $file . '" /></a></td>'; 
  $colCtr++; 
} 

Link to comment
Share on other sites

ProjectFear, Thank you for your reply.

 

How would i get the ID to increase for each image.

 


$images = "products/"; # Location of small versions 
$big    = "big/"; # Location of big versions (assumed to be a subdir of above) 
$cols   = 2; # Number of columns to display
[b]$id = 1 [/b]

if ($handle = opendir($images)) { 
   while (false !== ($file = readdir($handle))) { 
       if ($file != "." && $file != ".." && $file != rtrim($big,"/")) { 
           $files[] = $file; 
       } 
   } 
   closedir($handle); 
} 

$colCtr = 0; 

echo '<table width="212" cellpadding="2"><tr>'; 

foreach($files as $file) 
{ 
  if($colCtr %$cols == 0) 
    echo '</tr><tr><td colspan="2"></td></tr><tr>'; 
  echo '<td wdith=50% align="center"><a href="products.php?id=' . $id . '"><img border="0" class="gradualfader" src="' . $images . $file . '" /></a></td>'; 
  $colCtr++; 
} 

echo '</table>' . "\r\n"; 

 

@ Pikachu2000, Sorry if you see the 404 error, it doesn't work because i need the script to collect data from the database rather than load the bigger image.

 

 

Link to comment
Share on other sites

OKAY, I have managed to get the script working how i like it; here it is:

 

<?php 


$images = "products/"; # Location of small versions 
$big    = "big/"; # Location of big versions (assumed to be a subdir of above) 
$cols   = 2; # Number of columns to display
$id = 1;



if ($handle = opendir($images)) { 
   while (false !== ($file = readdir($handle))) { 
       if ($file != "." && $file != ".." && $file != rtrim($big,"/")) { 
           $files[] = $file; 
       } 
   } 
   closedir($handle); 
} 

$colCtr = 0; 

echo '<table width="212" cellpadding="2"><tr>'; 

foreach($files as $file)
{ 
  if($colCtr %$cols == 0) 
    echo '</tr><tr><td colspan="2"></td></tr><tr>'; 
  echo '<td wdith=50% align="center"><a href="products.php?id=' . $id . '"><img border="0" class="gradualfader" src="' . $images . $file . '" /></a></td>'; 
  $colCtr++; 
  $id++;
}

echo '</table>' . "\r\n"; 

?> 

 

This script selects and displays all the images from the folder into two neat columns and increases the ID by 1 each time. Thank you for pointing me in the right directio!!

 

My new problem now is actually using the ID.

I need to connect to the database and grab the corresponding row of data. Heres an example database:

 

ID  | name        | description  | price    | paypal

  1  | A product  | A description| £10.00 | <img src="">

  2  | B product  | B description| £15.00 | <img src="">

  3  | C product  | C description| £25.00 | <img src="">

 

So if the image link is products.php?id=2 then it grabs the 2nd row and populates the fields.

 

So far I have....

<?php
$con = mysql_connect("localhost","","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("", $con);

$sql = "SELECT * FROM Products WHERE personID = '$id'";
$result = mysql_query($sql);
$detail = mysql_fetch_assoc($result);
$productname = $detail["name"];

?>

<?php PRINT $productname; ?>

<?php PRINT $description; ?>

<?php PRINT $price; ?>

<?php PRINT $paypal; ?>

 

But this is not working, any ideas why?

 

Thanks in Advance

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.