need some help printing from database

Discussion in 'PHP' started by dougvcd, Feb 10, 2008.

  1. #1
    hi again
    need a little help with printing from database i have got it to print with javascipt
    but need some help on how to print it neater
    i presume it may be style sheet not sure
    this is how it prints at the moment

    1.... Digoxin .... Morning .... 1 .... 125 micr .... cardiac glycosides (slows heart)
    2.... Warfarin .... Evening .... 2 & 1 .... 1mg .... Anticoagulant (blood thiner)
    3.... Bumetanide .... Morning .... 2 .... 1mg .... diuretic (water Tablet)
    4.... Simvastatin .... Evening .... 1 .... 10mg .... hmg-coa reductase (colestral)
    5.... Bisoprolol Fumarate .... Morning .... 1 .... 5mg .... Beta Blocker (blood Pressure)

    this is the code i use to print
    would like it in colums so it all lines up
    cheers
    Doug
     
    dougvcd, Feb 10, 2008 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    print with javascript? Use a HTML table to format the data as it comes out of the db. something like

    <table>
    <tr><td>Field 1<td>Field 2......</tr></table>
    put it inside the loop for outputting the data. Table tags should be before and after the loop.
     
    shallowink, Feb 10, 2008 IP
  3. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for that will go and look at tables
    never used them before
    cheers
    Doug
    ok had a look and havnt got a clue if any one can help
     
    dougvcd, Feb 10, 2008 IP
  4. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #4
    It should look something like this ...

    echo "<table>";
    // start loop

    echo "<tr><td>$i<td>$info['name'] <td>$info['region']<td>$info['username'] <td>$info['password']";
    echo "<td>$info['email'] </tr>";
    $i++;
    }
    //end loop
    echo "</table>";

    quick explanation is:
    table ->starts the table
    tr->Table Row
    td->Cell in a row (think columns)
     
    shallowink, Feb 10, 2008 IP
  5. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    cant get this to work

    while($info = mysql_fetch_array( $data )) 
    { 
    //Outputs the image and other data
    echo "<table>";
    // start loop
    
    echo "<tr><td>$i<td>$info['name'] <td>$info['region']<td>$info['username'] <td>$info['password']";
    echo "<td>$info['email'] </tr>";
    $i++;
    }
    //end loop
    echo "</table>";
    
    
    ?> 
    PHP:
     
    dougvcd, Feb 10, 2008 IP
  6. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #6
    Is it showing anything? Errors? Did you check the source for the page?
    
    echo "<table>";
    
    // start loop
    while($info = mysql_fetch_array( $data ))   { 
    //Outputs the image and other data
    echo "<tr><td>$i<td>$info['name'] <td>$info['region']<td>$info['username'] <td>$info['password']";
    echo "<td>$info['email'] </tr>";
    $i++;
    }
    //end loop
    echo "</table>";
    
    ?> 
    PHP:
     
    shallowink, Feb 10, 2008 IP
  7. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    all i am getting is a blank page
    cheers
    Doug
     
    dougvcd, Feb 10, 2008 IP
  8. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #8
    Can you post the part where you make the database call. Cause the output part should be correct.
     
    shallowink, Feb 10, 2008 IP
  9. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    //Retrieves data from MySQL
      $data = mysql_query("SELECT * FROM shirleys_tablets") or die(mysql_error());
    //Puts it into an array 
    $i=01;
    while($info = mysql_fetch_array( $data ))  {
    PHP:
     
    dougvcd, Feb 10, 2008 IP
  10. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    here is the full code

    <?php
    
    // Connects to your Database 
    $dbh=mysql_connect("l", "c", "s") or die('I cannot connect to database because: ' .mysql_error()) ; 
    mysql_select_db("caravanh_users"); 
     
    //Retrieves data from MySQL
      $data = mysql_query("SELECT * FROM shirleys_tablets") or die(mysql_error());
    //Puts it into an array 
    $i=01;
    while($info = mysql_fetch_array( $data ))  
    {
    //Outputs the image and other data 
    Echo"<table>";
    // start loop
    Echo"<tr><td>$i<td>$info['name'] <td>$info['region'] <td>$info['username']  <td>$info['password'] ";
    Echo"<td>$info['email'] </tr>";
    $i++;
    
    }
    //end loop
    Echo "</table>";
    
    ?> 
    </p>
    <p><a href="javascript: window.print();">Print Out</a></p>
    <p><a href="index.php">Home</a> </p>
    </body>
    PHP:
     
    dougvcd, Feb 10, 2008 IP
  11. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #11
    Everythings the same as what you had working before correct? Did you check the HTML page source to see if there was something preventing it from displaying in the browser?
     
    shallowink, Feb 10, 2008 IP
  12. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #12
    ok i put both pages on server
    and yes the first one works ok
    but the table one dosnt
     
    dougvcd, Feb 10, 2008 IP
  13. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #13
    $dbh=mysql_connect("l", "c", "s") or die('I cannot connect to database because: ' .mysql_error()) ;
    l,c,s ? the first should be localhost, or do you mean for those to be variables?
     
    shallowink, Feb 10, 2008 IP
  14. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #14
    i didnt put the full addy to stop others from connecting
    cheers
    Doug
     
    dougvcd, Feb 10, 2008 IP
  15. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #15
    Well looks like you have to use this screwed up method to make it work:

    Echo"<tr><td>$i<td>" . $info['name'] . "<td>" . $info['region'] ."<td>" . $info['username'] ."<td>" . $info['password'] ;
    Echo"<td>". $info['email'] ." </tr>";
     
    shallowink, Feb 10, 2008 IP
  16. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #16
    ok all soted now
    thanks to all for the help
    Doug
    :D
     
    dougvcd, Feb 11, 2008 IP