1

I need a little help with some coding. I have a page named index.php and a picture.php.

index.php

$pid = 0;

for($ctr = 1; $ctr <= 2; $ctr++)
{
    echo '<tr>';
    $pid++;

    echo '<td>';
    echo '<a id="various3" href="picture.php" title="try" idno="5"><img src="picture.php" width="150" height="210">';
    //echo '<br>'.$pagenm;
    echo '</td>';

    echo '</td>';
    echo '</tr>';
}

After $pid++ is executed, I want its value to be sent to picture.php, so that it can be used for a retrieving query.

picture.php

$host = 'localhost';
$user = 'root';
$pw = '';
$db = 'thepillar';

mysql_connect($host, $user, $pw);
mysql_select_db($db);
$sql = "select pics, ext from infopics where id='5'";
// mysql_real_escape_string($sql, mysql_connect);
$result = mysql_query($sql) or die('Bad query at 12!' . mysql_error());
while ( $row = mysql_fetch_array($result, MYSQL_ASSOC) ) {
    $db_img = $row['pics'];
    $type = $row['ext'];
}
$img = base64_decode($db_img); // print_r($db_img );
$img = imagecreatefromstring($img);
header("Content-Type: image/jpeg");
imagejpeg($img);
imagedestroy($img);

I have only assigned 5 as the id to be used to retrieve the image from the database. This is because I don't know how to fetch $pid from index.php and assign it to a variable which I can use in my SQL query.

Any help would be much appreciated. Thanks in advance.

2
  • Are you grabbing picture.php with ajax? or just loading the page as a full regular page? Commented Mar 4, 2011 at 3:57
  • .i don't use ajax.. also because i don't know how. Commented Mar 4, 2011 at 4:05

3 Answers 3

1

index.php

for($ctr=1; $ctr<=2; $ctr++)
{
     echo '<tr><td><a id="various3" href="picture.php?id=', 
     // I use $ctr insteaf of $pid because is the same... starts with 1
     $ctr,'" title="try" idno="5"><img src="picture.php" width="150" height="210">',
     '</td></td></tr>';
}

picture.php

<?php
...
$sql = sprintf("select pics, ext from infopics where id='%d'", $_GET['id']);
...
?>
Sign up to request clarification or add additional context in comments.

2 Comments

.dude if i use $ctr instead of $pid the count will return to zero upon reaching the condition $ctr<=2. but A BIG THANKS TO YOU! this works FINE with me! specially the $sql = sprintf("select pics, ext from infopics where id='%d'", $_GET['id']);
why it will return to zero? i'm just printing it. you're welcome jaja
1

You could include it in the query string of picture.php, e.g.:

echo '<a id="various3" href="picture.php?id=', $pid, '" ... ';

Then in picture.php:

if (!isset($_GET['id']) || !ctype_digit($_GET['id'])) {
    // todo: some proper error handling
    exit;
}
$sql = "select pics, ext from infopics where id=" . $_GET['id'];

Comments

0

Don't store pictures in database, but save names only.
You won't need to pass anything anywhere and you'll avoid other silly mistakes from your code.

make your main page like this

<?php
$host = 'localhost';
$user = 'root';
$pw = '';
$db = 'thepillar';

mysql_connect($host,$user,$pw); 
mysql_select_db($db); 
$sql = "select pic from infopics limit 2";
$result = mysql_query($sql) or trigger_error(mysql_error().$sql); 
while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
  echo '<tr>';
  echo '<td>';
  echo '<a id="various3" href="/pictures/'.$row['pic'].'" title="try" idno="5">
        <img src="/pictures/thumbnails/'.$row['pic'].'" width="150" height="210">';
  echo '</td>';
  echo '</tr>';
} 
?>

that's all

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.