Page generating script

  • conorific
  • Proficient
  • Proficient
  • User avatar
  • Posts: 350
  • Loc: NY

Post 3+ Months Ago

I'm designing an art gallery's website. Is there a PHP or CGI script that could automatically pull up the ID number, description and price of each item? Wait, no, that's not clear.

I have thumbnails arranged on a page. When I click each thumbnail, I want JavaScript popups that open up a page that has the price, ID # and description. The reason I ask is because it's RIDICULOUS to make a page for every thumbnail...there are 250 or so.

If anyone knows, that'd be great.
  • Lieven
  • Beginner
  • Beginner
  • User avatar
  • Posts: 57
  • Loc: Belgium

Post 3+ Months Ago

I don't know by heart a script that does this. I also don't have a scriplet lying around... You should definately check out http://www.hotscripts.com I'm sure there's a script that does what you want (with only a little modification effort).
  • rjmthezonenet
  • Expert
  • Expert
  • User avatar
  • Posts: 526
  • Loc: St. John's, Newfoundland, Canada

Post 3+ Months Ago

Actually, what you want is a script that automatically builds a thumbnail page where each image is linked to details.php?item=xxxx. You might as well automate everything (and avoid some human errors along the way).

Is a database system installed on the server? If not, you can still manage this with a text file. With just:

id,price,description
xxxx,$xx.xx,ab ... xyz

...and your images saved as xxxx.jpg, you can pull of the works.

P.S. Avoid pop-ups if you can. You can't guarantee pop-ups aren't blocked. Furthermore, save your JavaScript when you need it and people with older browsers will thank you.
  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Posts: 4581
  • Loc: Maryland

Post 3+ Months Ago

Let's just say you have 100 images, because thats a nice...round number.

So you'll want to start off (to save time) creating the thumbnail page in PHP automatically. That can be done with the WHILE control structure. Now this first example will be assuming you are using standardized filenames for your thumbnails (*.jpg):

Code: [ Select ]
<html>
<body>
<table>
<tr>
<?

$count = 1; //set up to call jpgs and keep track of ID's.
$total_images = 100; //self explainitory
$imgrow = 0;

//now for the while structure
while ($count <= $total_images) {
   if ($imgrow == 4) { echo("</tr><tr>\n"); $imgrow = 0; }
   // that limits the table to having four images per row
   echo("<td>\n");
   echo("<a href='info.php?item=" . $count . "'>\n");
   echo("<img src='thumbs/" . $count . ".jpg' />\n");
   echo("</a></td>\n");
   //outputs the thumbnail within a cell
   $imgrow++; //
   $count++;   //increase both to keep while in check
}

echo ("</tr></table>\n");
?>

</body>
</html>
  1. <html>
  2. <body>
  3. <table>
  4. <tr>
  5. <?
  6. $count = 1; //set up to call jpgs and keep track of ID's.
  7. $total_images = 100; //self explainitory
  8. $imgrow = 0;
  9. //now for the while structure
  10. while ($count <= $total_images) {
  11.    if ($imgrow == 4) { echo("</tr><tr>\n"); $imgrow = 0; }
  12.    // that limits the table to having four images per row
  13.    echo("<td>\n");
  14.    echo("<a href='info.php?item=" . $count . "'>\n");
  15.    echo("<img src='thumbs/" . $count . ".jpg' />\n");
  16.    echo("</a></td>\n");
  17.    //outputs the thumbnail within a cell
  18.    $imgrow++; //
  19.    $count++;   //increase both to keep while in check
  20. }
  21. echo ("</tr></table>\n");
  22. ?>
  23. </body>
  24. </html>


That is basically a styless way of setting up all 100 (or 250) images easily. Pages and such makes it a bit more difficult (but not too hard).

So now you want to make info.php and have it query your mySQL database for the descritipon and price.

Code: [ Select ]
<html>
<body>
<?
//mysql stuff
$img_que = "SELECT * FROM art_table WHERE id = " . $item . " LIMIT 1";
$img_sql = mysql_query($img_que) or DIE (mysql_error());
$img_row = myql_fetch_array($img_sql);
// $img_row now contains the ID $img_row[0], desc $img_row[1]
// and price $img_row[2]

echo ("<img src='fullsize/" . $img_row[0] . ".jpg' /><br />\n");
echo ($img_row[1] . "<br />\n");
echo ('$' . $img_row[2] . "<br />\n");
?>
</body>
</html>
  1. <html>
  2. <body>
  3. <?
  4. //mysql stuff
  5. $img_que = "SELECT * FROM art_table WHERE id = " . $item . " LIMIT 1";
  6. $img_sql = mysql_query($img_que) or DIE (mysql_error());
  7. $img_row = myql_fetch_array($img_sql);
  8. // $img_row now contains the ID $img_row[0], desc $img_row[1]
  9. // and price $img_row[2]
  10. echo ("<img src='fullsize/" . $img_row[0] . ".jpg' /><br />\n");
  11. echo ($img_row[1] . "<br />\n");
  12. echo ('$' . $img_row[2] . "<br />\n");
  13. ?>
  14. </body>
  15. </html>


And thats pretty much a basic no fireworks way of doing it. Hope that helps :)
  • conorific
  • Proficient
  • Proficient
  • User avatar
  • Posts: 350
  • Loc: NY

Post 3+ Months Ago

I'm sorry to say I don't have a database and I don't know when I will...not to mention I haven't the slightest clue how to work them anyway, for the moment being. I'm pressed for time and I can't learn right now.

rjmthezonenet, what do you mean by a text file? I understand what you think I should do, and to be honest, I'm not sure why I didn't think of that. I just don't get the second part of what you said.

And I thought popup blockers only blocked popups that weren't generated by a click...
  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Posts: 4581
  • Loc: Maryland

Post 3+ Months Ago

alright..gotcha :-/ Sorry that couldn't help. rjmthezonenet was referring to storing all the information for each item on a line, then you go back and parse each line into an array, then you'de have to explode each array to a subarray and do a while statement to get each value. I suppose you could also be able to pick out which line you want.

I can probably throw something together if you want me to.
  • rjmthezonenet
  • Expert
  • Expert
  • User avatar
  • Posts: 526
  • Loc: St. John's, Newfoundland, Canada

Post 3+ Months Ago

You don't need a fully blown RDMS for the information you are recording (nothing against RDMSs). PHP can read a text file that has one item per line, and each line has several values which are seperated by a delimiter (comma, tab, etc.). A little loop runs through the text file, outputting appropriate HTML.

Assuming the text file contains:
id1,price1,description1
id2,price2,description2

your HTML may be something like:

(list.php page)
<ul>
<li><a href="details.php?item=id1"><img src="id1.jpg"></a>
<li><a href="details.php?item=id2"><img src="id2.jpg"></a>
</ul>

(details.php?item=id1 page)
<h1>id1</h1>
<p><img src="id1.jpg" align="right">description1<br>$price1</p>

This design as been extremely simplified for the sake of clarity. When I have some time, I'll post another version of the code that would work better.
  • conorific
  • Proficient
  • Proficient
  • User avatar
  • Posts: 350
  • Loc: NY

Post 3+ Months Ago

I get what you mean now. It's fine, you don't have to be more detailed. I relayed what you said to b_heyer and he's building me a script. Thank you so much and once I get this stupid thing off my shoulders, I'm picking up a freaking book and learning this. Sometimes I hate depending on others so heavily.

Thanks to all who replied!!!!
  • rjmthezonenet
  • Expert
  • Expert
  • User avatar
  • Posts: 526
  • Loc: St. John's, Newfoundland, Canada

Post 3+ Months Ago

b_heyer is right. The magic is in PHPs fgetcsv function and a while loop.

<?php
$row = 1;
$handle = fopen("test.csv", "r");
while ($data = fgetcsv($handle, 1000, ",")) {
$num = count($data);
echo "<p> $num fields in line $row: <br />\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
?>

http://ca.php.net/manual/en/function.fgetcsv.php
  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Posts: 4581
  • Loc: Maryland

Post 3+ Months Ago

In case you are all curious as to what I wrote here it is:

gallery.php
Code: [ Select ]
<html>
<body>
<table>
<tr>
<?

$data_file = 'http://conorific.com/ashleyidnum.txt';
$data_arr = file($data_file);

if (empty($page)){$page = 1;}

$total_images = count($data_arr);
$count = 0;
$page_cnt = 0;
$imgrow = 0;
$p = 1;

while ($count < $total_images) {
   if ($imgrow == 4) {
    $for_page[$p] .= "</tr><tr>\n";
    $imgrow = 0;
   }
   if ($page_cnt == 24) {
    $page_cnt = 0;
    $p++;
   }
   $for_page[$p] .= "<td>\n";
   $for_page[$p] .= "<a href='info.php?id=" . $count . "&&page=$page'>\n";
   $for_page[$p] .= "<img src='/thumb/" . $data_arr[$count] . ".jpg' />\n";
   $for_page[$p] .= "</a></td>\n";
   $imgrow++;
   $count++;
   $page_cnt++;
}

$total_pgs = count($for_page);
$tpc = 1;

while ($tpc <= $total_pgs) {
    if ($tpc == $page) {
        $for_page_navi .= " $tpc ";
    } else {
        $for_page_navi .= " <a href='?page=$tpc'>$tpc</a> ";
    }
    $tpc++;
}


echo ($for_page[$page]);

echo ("</tr><tr><td colspan=4>Page: $for_page_navi</td></tr></table>\n");
?>

</body>
</html>
  1. <html>
  2. <body>
  3. <table>
  4. <tr>
  5. <?
  6. $data_file = 'http://conorific.com/ashleyidnum.txt';
  7. $data_arr = file($data_file);
  8. if (empty($page)){$page = 1;}
  9. $total_images = count($data_arr);
  10. $count = 0;
  11. $page_cnt = 0;
  12. $imgrow = 0;
  13. $p = 1;
  14. while ($count < $total_images) {
  15.    if ($imgrow == 4) {
  16.     $for_page[$p] .= "</tr><tr>\n";
  17.     $imgrow = 0;
  18.    }
  19.    if ($page_cnt == 24) {
  20.     $page_cnt = 0;
  21.     $p++;
  22.    }
  23.    $for_page[$p] .= "<td>\n";
  24.    $for_page[$p] .= "<a href='info.php?id=" . $count . "&&page=$page'>\n";
  25.    $for_page[$p] .= "<img src='/thumb/" . $data_arr[$count] . ".jpg' />\n";
  26.    $for_page[$p] .= "</a></td>\n";
  27.    $imgrow++;
  28.    $count++;
  29.    $page_cnt++;
  30. }
  31. $total_pgs = count($for_page);
  32. $tpc = 1;
  33. while ($tpc <= $total_pgs) {
  34.     if ($tpc == $page) {
  35.         $for_page_navi .= " $tpc ";
  36.     } else {
  37.         $for_page_navi .= " <a href='?page=$tpc'>$tpc</a> ";
  38.     }
  39.     $tpc++;
  40. }
  41. echo ($for_page[$page]);
  42. echo ("</tr><tr><td colspan=4>Page: $for_page_navi</td></tr></table>\n");
  43. ?>
  44. </body>
  45. </html>


and info.php
Code: [ Select ]
<html>
<body>

<?

$data_file = "http://conorific.com/ashleydata1.txt";
$data_arr = file($data_file);

$i = $data_arr[$id];
$info = explode('*', $i);

echo("<img src='$info[0].jpg' /><br />ID: $info[0]<br />\n");
echo("$info[1]<br />\n");
echo("$info[2]<br />\n");

if ($id == 0) {
    $next_id = $id + 1;
    $next = "<a href='info.php?id=$next_id'>Next</a>\n";
} else if ($id == count($data_arr)) {
    $prev_id = $id - 1;
    $prev = "<a href='info.php?id=$prev_id'>Previous</a>\n";
} else {
        $prev_id = $id - 1;
        $next_id = $id + 1;
        $prev = "<a href='info.php?id=$prev_id'>Previous</a>\n";
        $next = "<a href='info.php?id=$next_id'>Next</a>\n";
}
$back = "<a href='gallery.php?page=$page'>Back to the Gallery</a>";
?>

<table>
<tr>
<td>
<? echo $prev; ?>
</td>
<td>
<? echo $back; ?>
</td>
<td>
<? echo $next; ?>
</td>
</tr>
</table>

</body>
</html>
  1. <html>
  2. <body>
  3. <?
  4. $data_file = "http://conorific.com/ashleydata1.txt";
  5. $data_arr = file($data_file);
  6. $i = $data_arr[$id];
  7. $info = explode('*', $i);
  8. echo("<img src='$info[0].jpg' /><br />ID: $info[0]<br />\n");
  9. echo("$info[1]<br />\n");
  10. echo("$info[2]<br />\n");
  11. if ($id == 0) {
  12.     $next_id = $id + 1;
  13.     $next = "<a href='info.php?id=$next_id'>Next</a>\n";
  14. } else if ($id == count($data_arr)) {
  15.     $prev_id = $id - 1;
  16.     $prev = "<a href='info.php?id=$prev_id'>Previous</a>\n";
  17. } else {
  18.         $prev_id = $id - 1;
  19.         $next_id = $id + 1;
  20.         $prev = "<a href='info.php?id=$prev_id'>Previous</a>\n";
  21.         $next = "<a href='info.php?id=$next_id'>Next</a>\n";
  22. }
  23. $back = "<a href='gallery.php?page=$page'>Back to the Gallery</a>";
  24. ?>
  25. <table>
  26. <tr>
  27. <td>
  28. <? echo $prev; ?>
  29. </td>
  30. <td>
  31. <? echo $back; ?>
  32. </td>
  33. <td>
  34. <? echo $next; ?>
  35. </td>
  36. </tr>
  37. </table>
  38. </body>
  39. </html>


I know it's not the neatest but it'll work.

rjm: I believe that file(); works well in this case (obviously) I have never used that function you mention before...which looks pretty cool, it's just a combo of file(); and explode(); right?
  • rjmthezonenet
  • Expert
  • Expert
  • User avatar
  • Posts: 526
  • Loc: St. John's, Newfoundland, Canada

Post 3+ Months Ago

yes, that is basically what it does. life is a little easier when the two are combined in a purpose built function.
  • conorific
  • Proficient
  • Proficient
  • User avatar
  • Posts: 350
  • Loc: NY

Post 3+ Months Ago

Code: [ Select ]
<?php include('head.txt'); ?>
<div align="center">
<?

$data_file = "http://conorific.com/sunny/ashleydata1.txt";
$data_arr = file($data_file);

$i = $data_arr[$_GET['id']];
$info = explode('*', $i);

echo("<img class='gall' src='images/ashley/card/$info[0].jpg'><br>ID: $info[0]<br />\n");
echo("$info[1]<br />\n");
echo("$info[2]<br />\n");

if ($_GET['id'] == 0) {
  $next_id = $_GET['id'] + 1;
  $next = "<a href='info.php?id=$next_id'>Next</a>\n";
} else if ($_GET['id'] == count($data_arr) - 1){
    $prev_id = $_GET['id'] - 1;
  $prev = "<a href='info.php?id=$prev_id'>Previous</a>\n";
} else {
     $prev_id = $_GET['id'] - 1;
     $next_id = $_GET['id'] + 1;
     $prev = "<a href='info.php?id=$prev_id'>Previous</a>\n";
     $next = "<a href='info.php?id=$next_id'>Next</a>\n";
}
$back = "<a href='gallery.php?page=$_GET['page']'>Back to the Gallery</a>";
?>

<br><br>
<? echo $prev; ?>|
<? echo $back; ?>|
<? echo $next; ?>

</div>

<?php include('foot.txt'); ?>
  1. <?php include('head.txt'); ?>
  2. <div align="center">
  3. <?
  4. $data_file = "http://conorific.com/sunny/ashleydata1.txt";
  5. $data_arr = file($data_file);
  6. $i = $data_arr[$_GET['id']];
  7. $info = explode('*', $i);
  8. echo("<img class='gall' src='images/ashley/card/$info[0].jpg'><br>ID: $info[0]<br />\n");
  9. echo("$info[1]<br />\n");
  10. echo("$info[2]<br />\n");
  11. if ($_GET['id'] == 0) {
  12.   $next_id = $_GET['id'] + 1;
  13.   $next = "<a href='info.php?id=$next_id'>Next</a>\n";
  14. } else if ($_GET['id'] == count($data_arr) - 1){
  15.     $prev_id = $_GET['id'] - 1;
  16.   $prev = "<a href='info.php?id=$prev_id'>Previous</a>\n";
  17. } else {
  18.      $prev_id = $_GET['id'] - 1;
  19.      $next_id = $_GET['id'] + 1;
  20.      $prev = "<a href='info.php?id=$prev_id'>Previous</a>\n";
  21.      $next = "<a href='info.php?id=$next_id'>Next</a>\n";
  22. }
  23. $back = "<a href='gallery.php?page=$_GET['page']'>Back to the Gallery</a>";
  24. ?>
  25. <br><br>
  26. <? echo $prev; ?>|
  27. <? echo $back; ?>|
  28. <? echo $next; ?>
  29. </div>
  30. <?php include('foot.txt'); ?>
  • rjmthezonenet
  • Expert
  • Expert
  • User avatar
  • Posts: 526
  • Loc: St. John's, Newfoundland, Canada

Post 3+ Months Ago

It has been a year since I looked at this code, and it was my very first week programming. Rather than just grabbing the code, consider how it fgetcsv is being used and write your own piece.

Code: [ Select ]
<?php
class Row2Array
{
  var $result;

  function Row2Array ($word, $table, $offset = "0") {
    $fpointer = fopen ($table,"r");
    $row = 1;
    while ($data = fgetcsv ($fpointer,1000)) {
      if ($row == 1) {
        $heading = $data;
      }
      else {
        if ($data[$offset] == $word) {
          $count = 0;
          foreach ($data as $value) {
            $tree[$heading[$count]] = $value;
          $count++;
          }
          break;
        }
      }
    $row++;
    }
    fclose ($fpointer);
    $this->result = $tree;
  }

  function ReturnData () {
    return $this->result;
  }
}
?>
  1. <?php
  2. class Row2Array
  3. {
  4.   var $result;
  5.   function Row2Array ($word, $table, $offset = "0") {
  6.     $fpointer = fopen ($table,"r");
  7.     $row = 1;
  8.     while ($data = fgetcsv ($fpointer,1000)) {
  9.       if ($row == 1) {
  10.         $heading = $data;
  11.       }
  12.       else {
  13.         if ($data[$offset] == $word) {
  14.           $count = 0;
  15.           foreach ($data as $value) {
  16.             $tree[$heading[$count]] = $value;
  17.           $count++;
  18.           }
  19.           break;
  20.         }
  21.       }
  22.     $row++;
  23.     }
  24.     fclose ($fpointer);
  25.     $this->result = $tree;
  26.   }
  27.   function ReturnData () {
  28.     return $this->result;
  29.   }
  30. }
  31. ?>


I included this class in a number of pages that needed to pull data from a text file that matches the matching criteria specified in $servicesdata.

Code: [ Select ]
$servicesdata = array(1, 2, 4, 5, 10, 12);
foreach ($servicesdata as $value) {
  $building = row2array ($value['building'], "../../db/building.txt");
  echo "<a href=\"../../db/index.php?bldg=".$building['id']."\" class=\"blocklink\">".$building['name']."</a>\n";
}
  1. $servicesdata = array(1, 2, 4, 5, 10, 12);
  2. foreach ($servicesdata as $value) {
  3.   $building = row2array ($value['building'], "../../db/building.txt");
  4.   echo "<a href=\"../../db/index.php?bldg=".$building['id']."\" class=\"blocklink\">".$building['name']."</a>\n";
  5. }


Sorting, matching, looping through the same text file over and over... yeah I know, I need a database and got one. That is why we were happy to toss out this code. (I actually wrote a more advanced class that could handle multiple text files and form relationships like SQL JOINS.) The whole thing was madness.

Now, in your case, every row of the text file is returned and it doesn't have to be in a specific order (you could pre-sort the text file). That being the case, you only need fgetcsv to read in each row and

Code: [ Select ]
$fpointer = fopen ("items.csv","r");
while ($data = fgetcsv ($fpointer,1000)) {
 echo "<a href=\"details\.php?id=".$data[0]."\"><img src=\"".$data[0]."\.jpg\"></a>\n";
}
fclose ($fpointer);
  1. $fpointer = fopen ("items.csv","r");
  2. while ($data = fgetcsv ($fpointer,1000)) {
  3.  echo "<a href=\"details\.php?id=".$data[0]."\"><img src=\"".$data[0]."\.jpg\"></a>\n";
  4. }
  5. fclose ($fpointer);


On the details page, your code would be something like:

Code: [ Select ]
$fpointer = fopen ("items.csv","r");
while ($data = fgetcsv ($fpointer,1000)) {
 if ($id == $data[0]) {
  echo "<p>\n";
  echo "<img src=\"".$data[0]."\.jpg\">\n";
  echo "<ul>\n";
  echo " <li>Price: ".$data[1]."\n";
  echo " <li>Description: ".$data[2]."\n";
  echo "</ul>\n";
 }
}
fclose ($fpointer);
  1. $fpointer = fopen ("items.csv","r");
  2. while ($data = fgetcsv ($fpointer,1000)) {
  3.  if ($id == $data[0]) {
  4.   echo "<p>\n";
  5.   echo "<img src=\"".$data[0]."\.jpg\">\n";
  6.   echo "<ul>\n";
  7.   echo " <li>Price: ".$data[1]."\n";
  8.   echo " <li>Description: ".$data[2]."\n";
  9.   echo "</ul>\n";
  10.  }
  11. }
  12. fclose ($fpointer);

Post Information

  • Total Posts in this topic: 13 posts
  • Users browsing this forum: No registered users and 102 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.