Closed Thread
Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 38

Thread: Displaying records from a mySQL DB

  1. #11
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Displaying records from a mySQL DB

    Quote Originally Posted by aakinn View Post
    How would I be able to specify a new row to start? as currently the data outputs in one continious row.

    I have put a width attribute on the table but this does not seem to make any difference. When I add more records it continues to carry on off the side of the page
    You'd simply use an if statement:

    Code:
    $i 0;
    while(
    $row=mysql_fetch_assoc($query)) {
        
    //format the output
        
    echo '<td><a  href="fullProduct.php?id='.$row['id'].'"><img   src="image.php?id='.$row['id'].'"></a><br />'.
            
    '<a   href="fullProduct.php?id='.$row['id'].'">'.$row['name'].'</a><br   />£'.$row['price'].'</td>';
        if((
    $i&#37;4)==3) {
            
    echo '</tr><tr>';
        }
        
    $i++;


  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #12
    aakinn is offline Newbie
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Re: Displaying records from a mySQL DB

    Ive not seen that clause in an if statement before what exactly does it doo? It dont look quite right to me and an error is being thrown on line 56

    Parse error: syntax error, unexpected T_ECHO in /home/ka751/public_html/WAT/products.php on line 56

    Line 56 being

    "echo '</tr><tr>';"

    But something is telling me its with the clause in the IF statement

  4. #13
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Displaying records from a mySQL DB

    Actually it messed up.. weird

    It's not suppose to be "if(($i&#37;4)==3) {" but rather "if(($i%4)==3) {"

  5. #14
    aakinn is offline Newbie
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Re: Displaying records from a mySQL DB

    That looks like something more familiar

    Would I not need to close the table tag at some point?

  6. #15
    aakinn is offline Newbie
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Re: Displaying records from a mySQL DB

    Ive now moved on somewhat and am trying to get some pagination going on. Its partially working in that it displays what page the user is currently on eg Page 1 of 2 but when I click next to naivgate to the next page the same results show but the variable $pagenum in the URL changes to 2

    Below is my code thus far

    Code:
    <?php
    $connection 
    mysql_connect ($host$user$passwd) or die ('Error connecting to mysql'); // connection to our mysql server
    mysql_select_db ($dbName) or die ('Error connecting to selected database'); //connection to our selected database
    //This checks to see if there is a page number. If not, it will set it to page 1
    if (!(isset($pagenum)))
    {
    $pagenum 1;
    }
    $query1 mysql_query("SELECT * FROM tblItems") or die(mysql_error());
    $rows mysql_num_rows($query1);
    //no results displayed per page
    $page_rows 4;

    //pg no of last page
    $last ceil($rows/$page_rows);

    if (
    $pagenum 1)
    {
    $pagenum 1;
    }
    elseif (
    $pagenum $last)
    {
    $pagenum $last;
    }

    // sets range to display query
    $max 'limit ' .($pagenum 1) * $page_rows .',' .$page_rows

    //display all products
    $query2 mysql_query("SELECT * FROM tblItems $max") or die(mysql_error());
    echo 
    '<table cellspacing="10"><tr>';
    $i 0;
    while(
    $row=mysql_fetch_assoc($query2)) {
        echo 
    '<td><a  href="fullProduct.php?id='.$row['id'].'"><img src="image.php?id='.$row['id'].'"></a><br />'.
            
    '<a   href="fullProduct.php?id='.$row['id'].'">Name: '.$row['name'].'</a><br   />Price: £'.$row['price'].'</td>';
        if((
    $i&#37;4)==3) {
        
    echo '</tr><tr>';
        }
        
    $i++;


    //  shows the user what page they are on and the total number of pages
    echo " Page $pagenum of $last<p>";
    //check if we are on page one. if not page one display tags
    if ($pagenum == 1)
    {
    }
    else
    {
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
    echo 
    " ";
    $previous $pagenum-1;
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
    }
    echo 
    " <hr /> ";

    // reverse above
    if ($pagenum == $last)
    {
    }
    else {
    $next $pagenum+1;
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
    echo 
    " ";
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";


    ?>
    What exaclty is going wrong?

  7. #16
    dp26 is offline Newbie
    Join Date
    Mar 2010
    Posts
    13
    Rep Power
    0

    Re: Displaying records from a mySQL DB

    I think I want to accomplish the same thing as your fullproduct.php.

    http://northstarseries.com/tmp/phptest.php

    I want the team name to link to the full team information. Is this similar?

  8. #17
    Arctic Fire is offline Learning Programmer
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Re: Displaying records from a mySQL DB

    Quote Originally Posted by dp26 View Post
    I think I want to accomplish the same thing as your fullproduct.php.

    http://northstarseries.com/tmp/phptest.php

    I want the team name to link to the full team information. Is this similar?
    Yea, pretty much the same concept. Use the ID in the query string to get the specific team's information.

    Also, when you're passing an ID in the query string like that, and it's just a number, you can just cast it to an int. BlaineSch commented in his code to filter it, and I'm just following up on that. You can never over stress security.

    Something like this will do just fine.

    Code:
    $id = (int)$_GET['id'];// FILTERED YAY!! 
    If you pass text though it, it will turn into a 0.

  9. #18
    dp26 is offline Newbie
    Join Date
    Mar 2010
    Posts
    13
    Rep Power
    0

    Re: Displaying records from a mySQL DB

    Do I have to have a different php file or do i run that query as phptest?id=1 ?

  10. #19
    Arctic Fire is offline Learning Programmer
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Re: Displaying records from a mySQL DB

    You have two separate files. One file will list all of the teams. The other file will give specific details for a team that you specify with an ID.

  11. #20
    dp26 is offline Newbie
    Join Date
    Mar 2010
    Posts
    13
    Rep Power
    0

    Re: Displaying records from a mySQL DB

    So I should write a teams.php that will extract out what I want. Makes sense. Thank you.

Closed Thread
Page 2 of 4 FirstFirst 1234 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. mysql records
    By tontonskie in forum Database & Database Programming
    Replies: 4
    Last Post: 12-13-2010, 04:18 PM
  2. Displaying MySQL data in HTML with PHP
    By dunnkers in forum PHP Development
    Replies: 4
    Last Post: 07-16-2010, 08:53 AM
  3. How can I delete mySQL records from an html or jsp page?
    By bigz008 in forum Database & Database Programming
    Replies: 2
    Last Post: 03-05-2010, 09:37 AM
  4. displaying mysql data into jtextfield.Pls help
    By iz.ziffa in forum Java Help
    Replies: 1
    Last Post: 05-31-2007, 08:58 AM

Tags for this Thread

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