943,513 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1773
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 5th, 2009
0

How do you get php to display more than one image from database?

Expand Post »
I'm trying to display in a web page all the images stored in a database. THis is my code, but it only displays the first picture:

php Syntax (Toggle Plain Text)
  1. <?php
  2. include "cysylltiad.php";
  3. $result = mysql_query("SELECT * FROM files ORDER BY fid");
  4. while($row = mysql_fetch_array($result)){
  5. header("Content-Type: {$row['type']}");
  6. echo $row["content"];
  7. }
  8. ?>

where content is the name of the picture field.

Any help would be appreciated.
Last edited by peter_budo; Mar 8th, 2009 at 7:05 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
twmprys is offline Offline
8 posts
since Oct 2008
Mar 5th, 2009
0

Re: How do you get php to display more than one image from database?

hi,

are you storing the images in the database or just the path to the images?

Also I always set the query to die if it fails and output the mysql error when it does IE

php Syntax (Toggle Plain Text)
  1. $result = mysql_query("SELECT * FROM files ORDER BY fid") or die(mysql_error());


Richard
Last edited by peter_budo; Mar 8th, 2009 at 7:06 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
rickya100 is offline Offline
78 posts
since Mar 2008
Mar 5th, 2009
0

Re: How do you get php to display more than one image from database?

Hi, thanks for responding. I'm storing the images actually in the database. I want to be able to allow a user to upload pictures with captions, then output all the pictures with their associated captions on a page.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
twmprys is offline Offline
8 posts
since Oct 2008
Mar 5th, 2009
0

Re: How do you get php to display more than one image from database?

Click to Expand / Collapse  Quote originally posted by twmprys ...
I'm trying to display in a web page all the images stored in a database. THis is my code, but it only displays the first picture:

<?php
include "cysylltiad.php";
$result = mysql_query("SELECT * FROM files ORDER BY fid");
while($row = mysql_fetch_array($result)){
header("Content-Type: {$row['type']}");
echo $row["content"];
}
?>

where content is the name of the picture field.

Any help would be appreciated.
in my mind, this should produce a row of images
the content-length header should tell the browser where to break the image,
untested, I do not use blobs
blobs in databases are slow, processor intensive and unneccesarily large, my databases just store the file system pointer to the image
php Syntax (Toggle Plain Text)
  1. while($row = mysql_fetch_array($result)){
  2. header("Content-Type: {$row['type']}");
  3. header("Content-Length: strlen($row['content'])");
  4. echo $row['content'];
  5. }
Last edited by almostbob; Mar 5th, 2009 at 8:38 pm.
Reputation Points: 562
Solved Threads: 367
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009
Mar 6th, 2009
0

Re: How do you get php to display more than one image from database?

Hi there!

I have a nice piece of code I use for queries like this, take a look.

php Syntax (Toggle Plain Text)
  1. function mysql_fetch_values($result, $numass=MYSQL_BOTH)
  2. {
  3. $i=0;
  4. $keys=array_keys(mysql_fetch_array($result, $numass));
  5. mysql_data_seek($result, 0) ;
  6. while ($row = mysql_fetch_array($result, $numass)){
  7. foreach ($keys as $speckey)
  8. $got[$i][$speckey]=$row[$speckey];
  9. $i++;
  10. }
  11. return $got;
  12. }

This enables you to fetch results in the form $result[row][column]. So if you have a database holding images and want to display all of them you would include this function and then use:

php Syntax (Toggle Plain Text)
  1. $result = mysql_fetch_values(mysql_query("SELECT * FROM files ORDER BY fid"));
  2.  
  3. foreach ($result as $image)
  4. echo $image['content'];

Hope this helps!
Last edited by peter_budo; Mar 8th, 2009 at 7:06 am. Reason: Just adding code tags for second code example
Reputation Points: 10
Solved Threads: 0
Light Poster
danielpataki is offline Offline
28 posts
since Oct 2008
Mar 6th, 2009
0

Re: How do you get php to display more than one image from database?

php Syntax (Toggle Plain Text)
  1. Function output_array14(){// by default we show first page
  2. $page = 1;
  3. // If current page number, use it
  4. // if not, set one!
  5. //echo $_GET['page'];
  6. if(!isset($_GET['page'])){
  7. $page = 1;
  8. } else {
  9. $page = $_GET['page'];
  10. }
  11.  
  12. // Define the number of results per page
  13. $max_results = 13;
  14.  
  15. // Figure out the limit for the query based
  16. // on the current page number.
  17. $from = (($page * $max_results) - $max_results);
  18. //echo $from;
  19. // Perform MySQL query on only the current page number's results
  20. //echo $from,$max_results;
  21.  
  22.  
  23. $sql = mysql_query("SELECT * FROM Garant WHERE Portfolio='existing' LIMIT $from, $max_results") ;
  24. //$sql = mysql_query("SELECT * FROM Garant");
  25. //$query= "SELECT * FROM Garant order by Price ASC"
  26. $result = mysql_query($sql);
  27.  
  28. while($row = mysql_fetch_array($sql))
  29. {
  30. // Build your formatted results here.
  31. $count = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM Garant"),0);
  32. if($count > 0) {
  33. $max=4; //set the number of images across here
  34.  
  35. echo "<table>";
  36.  
  37. for($i=0;$i<$max_results;$i+=$max) /* loop through and build the table */
  38. {
  39. $headings = $pics = $locations = '<tr>';
  40. for($j=0;$j<$max;$j++)
  41. {
  42. if($row = mysql_fetch_assoc($sql))
  43. {
  44.  
  45. //$pics .="<TD bgcolor=\"#FFFFFF\" width=\"125\" ><A HREF=\"details.php?detail=".$images["Ref"]." \"><IMG SRC=\"resources/".$images["smallpic"]."\"></A></TD>";
  46. $pics .="<TD bgcolor=\"#B74F8F\" border=\"2\"><A HREF=\"details.php?detail=".$row["Ref"]." \"><IMG SRC=\"resources/".$row["smallpic"]."\"></A></TD>";
  47. //$locations .= '<td align="center" bgcolor=#DDEEFF><B>' .$images['Price']. ' Euro - ref - ' .$images['Ref'].'</td>';
  48. $locations .= '<td align="left" bgcolor=#DDEEFF><B> ' .$row['Ref']. ' Price: ' .$row['Price'].' Euro</td>';
  49. } else {
  50. // $headings .= '<td>&nbsp;</td>';
  51. $pics .= '<td>&nbsp;</td>';
  52. $locations .= '<td>&nbsp;</td>';
  53. }
  54. }
  55. $headings .= "</tr>\n";
  56. $pics .= "</tr>\n";
  57. $locations .= "</tr>";
  58. echo $headings;
  59. echo $pics;
  60. echo $locations;
  61. }
  62. echo '</table>';
  63. }
  64. }
  65.  
  66. // Figure out the total number of results in DB:
  67. $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM Garant"),0);
  68.  
  69. // Figure out the total number of pages. Always round up using ceil()
  70. $total_pages = ceil($total_results / $max_results);
  71. //echo $total_pages, $total_results, $max_results;
  72. // Build Page Number Hyperlinks
  73. echo "<center>Select a Page<br />";
  74.  
  75. // Build Previous Link
  76. if($page > 1){
  77. $prev = ($page - 1);
  78. echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
  79. }
  80.  
  81. for($i = 1; $i <= $total_pages; $i++){
  82. if(($page) == $i){
  83. echo "$i ";
  84. } else {
  85. echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
  86. }
  87. }
  88.  
  89. // Build Next Link
  90. if($page < $total_pages){
  91. $next = ($page + 1);
  92. echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
  93. }
  94. echo "</center>";
  95.  
  96. }
  97. mysql_free_result($result) ;
  98.  
  99. ?>
Last edited by peter_budo; Mar 8th, 2009 at 7:07 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 0
Light Poster
phobia1 is offline Offline
25 posts
since Aug 2007
Mar 6th, 2009
0

Re: How do you get php to display more than one image from database?

No, that doesn't work - nothing shows now. I'm obviously missing something. I think I read somewhere on the net that you need one page to call up the pictures, and another to display them but I can't find an explanation of that anywhere.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
twmprys is offline Offline
8 posts
since Oct 2008
Mar 6th, 2009
0

Re: How do you get php to display more than one image from database?

Tutorial:: get multiple images from sql blobs

its tedious
now I'm really glad I dont store blobs in the database
Reputation Points: 562
Solved Threads: 367
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009
Mar 6th, 2009
0

Re: How do you get php to display more than one image from database?

I'm not a huge expert on database speed, but I believe it is recommended NOT to use BLOB a lot because file systems are much more quicker at handling files.

Personally I never use blogs, I store the filename in the database. I use the code I posted above to cycle through all the file names and show them on site.
Reputation Points: 10
Solved Threads: 0
Light Poster
danielpataki is offline Offline
28 posts
since Oct 2008
Mar 6th, 2009
0

Re: How do you get php to display more than one image from database?

I've never stored images as BLOB in a DB as every article I've ever read says to not do. I find it much easier (once you learn it) just storing the path in the database and storing the actual files in a folder.

I know that doesn't help. If it's possible I would change to just storing the path, if not I hope someone with experience of BLOBs can help.

Good luck
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
rickya100 is offline Offline
78 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: what is best method to get response from a service?
Next Thread in PHP Forum Timeline: Session share across multiple domains same IP





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC