Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

Images in a table

images php coding issues or problems here.

Moderators: macek, egami, gesf

Images in a table

Postby jonesyp » Mon Apr 04, 2011 11:21 am

Hello,

I am fairly new to PHP and am trying to display images in a table. I have found a script to which I have added my connection details which would in other scripts.

I created a table called images with columns called upload_time, src, and title, and stored for gif files into the table. The images are in the same folder as the PHP file.
SQL result

Host: sql103.beesimple.co.uk
Database: beesi_7131470_school

SQL query: SELECT * FROM `images` LIMIT 0, 30 ;
Rows: 5
src title upload_time
A-10Thunderbolt.gif A-10Thunderbolt.gif 2011-04-04 18:47:42
A-10Thunderbolt.gif A-10Thunderbolt.gif 2011-04-04 18:47:50
A-BSide.gif A-BSide.gif 2011-04-04 18:48:14
A-Centauro.gif A-Centauro.gif 2011-04-04 18:48:37
A-Maze.gif A-Maze.gif 2011-04-04 18:48:54

The PHP File is as follows:


<?php

//connect to MySQL; note we’ve used our own parameters- you should use

//your own for hostname, user, and password

$connect = mysql_connect("sql103.beesimple.co.uk", "XXX", "XXX") or

die ("Hey loser, check your server connection.");

mysql_select_db("beesi_7131470_school");

$pull_images = mysql_query("SELECT * FROM `images` ORDER BY `upload_time` ASC")


if( @mysql_num_rows( $pull_images ) > 0 ) {
echo '<<strong class="highlight">table</strong>>';
$count = 0;
while( $row = mysql_fetch_array( $pull_images ) ) {
if( $count == 0 ) {
echo "<tr><td>";
} else {
echo "<td>";
}
echo '<img src="' . $row['src'] . '" alt="' . $row['title'] . '" />';
if( $count == 3 ) {
echo '</td></tr>';
$count = 0;
} else {
echo '<td>';
$count++;
}
}

$cells_left = 4 - $count;
if( $cells_left > 0 ) {
$i = 0;
while( $i <= $cells_left ) {
echo '<td></td>';
$i++;
}
echo '</tr>';
}
echo '</table>';
} else {
echo "No <strong class="highlight">images</strong> <strong class="highlight">in</strong> the database.";
}

?>



Many thanks

Peter Jones
jonesyp
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Apr 04, 2011 11:12 am

Re: Images in a table

Postby egami » Mon Apr 04, 2011 12:17 pm

A-10 Thunderbolt.. my favorite!
Code: Select all


$maxrow 
5;
$rowcount 1;

$query "SELECT src FROM images ORDER BY upload_date";
$result mysql_query($query);

if (
mysql_num_rows($result) > 0) { 
  echo 
'
  <div>
  <table>
  '
;
  while (
$row mysql_fetch_assoc($result)) { 
    if (
$rowcount == 1) { 
      echo 
'<tr>';
    }
    if (
$rowcount <= $maxrow) { 
      echo 
'<td><img src="$row[src]" border="0"></td>';
    }
    if (
$rowcount == $maxrow) { 
      echo 
'</tr>';
    }
    
$rowcount++;
    if (
$rowcount $maxrow) { 
      
$rowcount 1;
    }
  }
  if (
$rowcount != $maxrow || $rowcount != 1) { 
    for(
$a $rowcount$a <= $maxrow$a++) { 
      echo 
'<td>&nbsp;</td>';
    }
    echo 
'</tr>';
  }
  echo 
'
  </table>
  </div>
  '
;
} else { 
  echo 
"Search returned 0 results.<br/>";
}
 
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Images in a table

Postby jonesyp » Mon Apr 04, 2011 12:32 pm

Thanks for this,

I have changed the script to the one below, but I now get no images.

http://buzz.beesimple.co.uk/htmltable/table.php


<?php

//connect to MySQL; note we’ve used our own parameters- you should use

//your own for hostname, user, and password

$connect = mysql_connect("sql103.beesimple.co.uk", "XXX", "XXX") or

die ("Hey loser, check your server connection.");

mysql_select_db("beesi_7131470_school");




$maxrow = 5;
$rowcount = 1;

$query = "SELECT src FROM images ORDER BY upload_time";
$result = mysql_query($query);

if (mysql_num_rows($result) > 0) {
echo '
<div>
<table>
';
while ($row = mysql_fetch_assoc($result)) {
if ($rowcount == 1) {
echo '<tr>';
}
if ($rowcount <= $maxrow) {
echo '<td><img src="$row[src]" border="0"></td>';
}
if ($rowcount == $maxrow) {
echo '</tr>';
}
$rowcount++;
if ($rowcount > $maxrow) {
$rowcount = 1;
}
}
if ($rowcount != $maxrow || $rowcount != 1) {
for($a = $rowcount; $a <= $maxrow; $a++) {
echo '<td>&nbsp;</td>';
}
echo '</tr>';
}
echo '
</table>
</div>
';
} else {
echo "Search returned 0 results.<br/>";
}


?>
jonesyp
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Apr 04, 2011 11:12 am

Re: Images in a table

Postby egami » Mon Apr 04, 2011 12:45 pm

add this to the end of the query

$result = mysql_query($query) or die ("Something's wrong with the query: ".mysql_error());
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Images in a table

Postby egami » Mon Apr 04, 2011 12:46 pm

also..
make sure that the images are in the same directory, otherwise you'll have to alter your <img src=*> tag..

ie..
<img src="uploads/$row[src]" border="0">

or even check to see if src is the right table name..

Maybe it's something else?
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Images in a table

Postby egami » Mon Apr 04, 2011 12:55 pm

Ok..
so I checked your site..
and it's echoing the string data strait to the webserver.

change this..
echo '<td><img src="$row[src]" border="0"></td>';

to this
echo '<td><img src="'.$row['src'].'" border="0"></td>';
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Images in a table [FIXED]

Postby jonesyp » Mon Apr 04, 2011 1:16 pm

Thats fab. Thanks so much. Roll on my Sinclair ZX Spectrum site!

Best wishes

Peter Jones
jonesyp
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Apr 04, 2011 11:12 am


Return to PHP coding => Images

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron