PHP MYSQL code problem

Discussion in 'PHP' started by mendoz, Sep 25, 2006.

  1. #1
    Hey there.

    I'm having a difficulty with a php mysql script.

    I need a page which shows only 6 pictures.
    The adresses of the images are stored in a database.

    let's say I have 10 images.
    I want the first page to link to a new page with the remaining four images.

    first page:
    [image] [image] [image]
    [image] [image] [image]
    next

    second page:
    [image] [image] [image]
    [image]
    previous

    Here is the code I got from another forum:
    <?php
    mysql_connect( "localhost", "username", "password" ); // Use correct stuff there
    mysql_select_db( "images" ); // Use Database Name
    $max_images = 8;
    if( !isset( $_GET[ 'page' ] ) ) {
    $page = 1;
    }
    else {
    $page = $_GET[ 'page' ];
    }
    $from = ($page * $max_images) - $max_images;
    
    // select the image url's from the db, i_id = the id of the row in the mysql db
    $sql = "SELECT * FROM images ORDER BY i_id LIMIT $from, $max_images";
    $s = mysql_query( $sql );
    while( $res = mysql_fetch_array( $s ) ) {
    $i = 1;
    //example variables, might be different for you
    
    // assuming the row name is "src"
    $src = $res[ 'src' ];
    
    echo '<img> ';
    if( $i > 4 ) {
     echo '<br>';
    }
    $i++;
    }
    echo '<br>';
    $total_images = mysql_result( mysql_query( "SELECT COUNT( i_id ) as Num FROM images" ), 0 );
    $total_pages = ceil( $total_images / $max_images );
    // previous page
    if($page > 1){
     $prev = ( $page - 1 );
       echo '<a>Previous</a> ';
    }
    
    for( $i = 1; $i <= $total_pages; $i++ ) {
    if( ( $page ) == $i ) {  
     echo "$i ";
    }
    else {
     echo '<a>$i</a> ';
    }
    }
    // next page
    if( $page </a>
    Code (markup):
    Now, I don't want an image, I want an include.
    The include is a table with variables (for the image and link) - table.txt.
    I want only to show up to 6 includes in a page.
    If there are no more includes I want to include 'soon.txt'

    First page:
    | include 'table.txt' | | include 'table.txt' | | include 'table.txt' |
    | include 'table.txt' | | include 'table.txt' | | include 'table.txt' |
    next page

    Second page:
    | include 'table.txt' | | include 'table.txt' | | include 'table.txt' |
    | include 'table.txt' | | include 'soon.txt' | | include 'soon.txt' |
    previous page

    Hope you undestood and could help,
    Dror Wolmer
     
    mendoz, Sep 25, 2006 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are you getting any errors with the current code? Are the tables stored in the DB?
     
    mad4, Sep 25, 2006 IP
  3. mendoz

    mendoz Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm not getting errors, but that's not what I wanted.

    I have a .txt file with a table in it written in html.
    I want to include that file up to 6 times in a single page.
    The number of tables to include is stored in a database.
     
    mendoz, Sep 25, 2006 IP
  4. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The code for an include is include('filename.txt');
     
    mad4, Sep 25, 2006 IP
  5. mendoz

    mendoz Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I know mad4, it was an example of how I want it to show the tables.
    I need help with the script.
     
    mendoz, Sep 25, 2006 IP
  6. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Which particular part of the script do you need help with?
     
    mad4, Sep 25, 2006 IP
  7. mendoz

    mendoz Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    well,

    the script is for showing images.

    I think I'll make a page called 'tables.php' with a table in it with two rows and three columns,
    inside every cell I'll put an <? include ('table.php'); ?>.
    'tables.php' will connect to a specific database via a var ('tables.php?cat=cat1').
    Then I want it to check how many rows are in that database.

    I want up to 6 <? include ('table.php'); ?>. in a page.
    So, if for example I have 10 rows in database 'cat1',
    I'll have a page with 6 <? include ('table.php'); ?>,
    and a second page with 4 <? include ('table.php'); ?>,
    And two <? include ('table-no.php'); ?>,

    For example:

    'tables.php?cat=cat1?id=0':
    <? include ('table.php'); ?>, <? include ('table.php'); ?>, <? include ('table.php'); ?>,
    <? include ('table.php'); ?>, <? include ('table.php'); ?>, <? include ('table.php'); ?>,
    next

    'tables.php?cat=cat1?id=1':
    <? include ('table.php'); ?>, <? include ('table.php'); ?>, <? include ('table.php'); ?>,
    <? include ('table.php'); ?>, <? include ('table-no.php'); ?>, <? include ('table-no.php'); ?>,
    previous
     
    mendoz, Sep 25, 2006 IP