PDA

View Full Version : PHP and MySQL table



calumogg
10-24-2007, 10:42 AM
Hi, I am using this script to display pictures from a database, the PHP script is supposed to display them in a table, but the pictures are not displaying or linking, here is a example of what is happening http://www.calumogg.co.uk/galleries.php?gallery=landscapes
Here is the code I am using:


<?php
$gallery = $_GET['gallery'];

$host="*****"; // Host name
$username="*****"; // Mysql username
$password="*****"; // Mysql password
$db_name="*****"; // Database name
$tbl_name="$gallery"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$query = "SELECT * FROM $tbl_name ORDER BY file_name DESC";
$result = mysql_query($query) or die('Error, query failed');

echo '<table width="100%" border="0" align="center" cellpadding="5">';

$colsPerRow = 4;

// width of each column in percent
$colWidth = (int)(100/$colsPerRow);

while ($row = mysql_fetch_assoc($result)) {
if ($i % $colsPerRow == 0) {
// start a new row
echo '<tr>';
}

echo '<td width="' . $colWidth . '%"><a href="' . $rows['large'] . '" rel="lightbox[' . $gallery . ']" title="' . $rows['file_name'] . '"><img src="' . $rows['thumbnail'] . '"></a></td>';

if ($i % $colsPerRow == $colsPerRow - 1) {
// end this row
echo '</tr>';
}

$i += 1;
}

// print blank columns
if ($i % $colsPerRow != 0) {
while ($i++ % $colsPerRow != 0) {
echo '<td width="' . $colWidth . '%">&nbsp;</td>';
}
echo '</tr>';
}

echo '</table>';
?>

Thanks in advance for any help :)

boogyman
10-24-2007, 12:28 PM
$gallery = $_GET['gallery'];
...
$tbl_name = "$gallery";


1) why the duplicity? its really just extranaeous coding
2) just uncomment the $gallery. it is likely the the interpreter is taking that as a string rather then the variable as intended.

if you wanted to keep the 2 variables you could do something like


$tbl_name = $gallery = $_GET['gallery'];

but again it really is not necessary

calumogg
10-24-2007, 01:39 PM
Thanks for the reply, but that bit of the code is working fine, the bit that isn't working is the part that is generating the table.

boogyman
10-24-2007, 01:54 PM
try to see what is actually being returned



while ($row = mysql_fetch_assoc($result)) {
print_r($row);
if ($i % $colsPerRow == 0) {
// start a new row
echo '<tr>';
}

echo '<td width="' . $colWidth . '%"><a href="' . $rows['large'] . '" rel="lightbox[' . $gallery . ']" title="' . $rows['file_name'] . '"><img src="' . $rows['thumbnail'] . '"></a></td>';

if ($i % $colsPerRow == $colsPerRow - 1) {
// end this row
echo '</tr>';
}
$i += 1;
}


that will tell you what is actually being returned, and thus tell you what is held in the "thumbnail"... if nothing is there well then you would need to check to make sure you have the field name correct and check to make sure that there is something in that record field

calumogg
10-24-2007, 02:28 PM
Iv just added that code and the data being retrieved, it just isn't going into the table.

boogyman
10-24-2007, 03:07 PM
I am just trying to help you troubleshoot as to what is actually being retrieved.

your script looks correct, which is why i offered that method in the first place... print_r() is going to print the key and contents of the array being returned, I did that so you could see what is actually in the "thumbnail" key and i assume that you are capable of figuring out how to modify it from there.

if its not what you were expecting

check the table name, the record identification, and the field name, and if you need to hard code anything else in, whether it be some pre-text or the extension...


and as for it going to other places.... well thats fine, but your problem is where the image should be printed right? if not then I misunderstood the question and can you please rephrase

calumogg
10-24-2007, 03:21 PM
Thanks for the help, when I added in print_r it showed that all the data was correct and it was being retrieved, but then when the PHP code comes to writing the table it knows there is a picture there but no of the data from the database is being put into the code, if you have a look at http://calumogg.co.uk/galleries.php?gallery=landscapes
you will see what I mean, also you can have a look at the source code and see whats happening there.
Thanks for sticking with it even if I cant explain it very clearly!!

boogyman
10-24-2007, 03:34 PM
$query = "SELECT * FROM $tbl_name ORDER BY file_name DESC";
$result = mysql_query($query) or die('Error, query failed');


you need to add a where clause


$query = "SELECT large, file_name, thumbnail FROM ".$tbl_name." WHERE _field_ = '_condition_' ORDER BY file_name DESC";


also you should add an alternate description so if the thumbnail cannot be displayed the user will have an idea of what its supposed to be, and thus would know if he/she would like to view the full image. and not everyone will think about hovering over the empty space to see the title that you have assigned. if someone goes onto a page and sees all empty parts like that well they are likely to navigate somewhere else, but if there is a textual representation they would be much more likely to click on one/more of the picture(s)


<img src='".$row['thumbnail'].'" alt="'.$row['file_name'].'">

calumogg
10-24-2007, 05:48 PM
Oooo I just found the error

In the code that I was using it says $rows I changed this to $row and it works!!

Thanks for all the help even if I wasnt being very clear.