Results 1 to 9 of 9

Thread: PHP and MySQL table

  1. #1
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default PHP and MySQL table

    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....ery=landscapes
    Here is the code I am using:

    PHP Code:
    <?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

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by you
    PHP Code:
    $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
    PHP Code:
    $tbl_name $gallery $_GET['gallery']; 
    but again it really is not necessary

  3. #3
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    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.

  4. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    try to see what is actually being returned

    Code:
    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

  5. #5
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    Iv just added that code and the data being retrieved, it just isn't going into the table.

  6. #6
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    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

  7. #7
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    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!!

  8. #8
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by you
    $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
    PHP Code:
    $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)
    Code:
    <img src='".$row['thumbnail'].'" alt="'.$row['file_name'].'">
    Last edited by boogyman; 10-24-2007 at 03:40 PM.

  9. #9
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •