+ Post New Thread
Results 1 to 8 of 8
Web Development Thread, Echo Results to nice format in Coding and Web Development; I have a php script which i am going to hopefully use for my noticeboard which i have been creating. ...
  1. #1

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,821
    Thank Post
    272
    Thanked 1,140 Times in 1,036 Posts
    Rep Power
    351

    Echo Results to nice format

    I have a php script which i am going to hopefully use for my noticeboard which i have been creating. I have a script which imports to the database. I also have one which is displaying the contents of the mysql table. But i can't seem to work out how to set it up so that it displayes in a nice format.

    I have this

    PHP Code:
    while($result mysql_fetch_array($query))
    { echo 
    $result['subject']; echo $result['name'];echo $result['post']; echo $result['expire'];} 
    but that just is displaying everything.

    I have tried everything by trying to echo it different things like tables etc.

    but cant't seem to work out what is wrong!

  2. #2

    bladedanny's Avatar
    Join Date
    May 2009
    Location
    Sheffield
    Posts
    1,314
    Thank Post
    191
    Thanked 313 Times in 234 Posts
    Rep Power
    171
    Could you put the results into a new variable then use the new variable where ever and how ever you'd like to?

    ---- Ignore this ---- I've thought about it and it woudln't work. I jumped the gun on my suggestion. Sorry.

  3. #3

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,771
    Thank Post
    1,511
    Thanked 1,294 Times in 884 Posts
    Rep Power
    813
    Try using 'printf' instead to add some labels (I belive, but I'm not certain, you can also add HTML tags to pretty it up):

    Code:
    while($result = mysql_fetch_array($query)) {
    printf("Subject: %s - Name: %s - Post: %s", $result["subject"], $result["name"], $result["post"]);
    }
    The first parameter is your formatted text, with each result represented by %s, the order in which the following parameters are called defines which order they are returned to the first parameter.

    EDIT: scrap that, I'm talking rubbish again. Just echo in to an HTML table? e.g.

    Code:
    <table>
    while($result = mysql_fetch_array($query)) 
    { echo "<tr><td>$result['subject']</td><td>$result['name']</td><td>$result['post']</td><td>$result['expire']</td></tr>";}  
    </table>
    Last edited by LosOjos; 9th February 2011 at 12:06 PM.

  4. #4

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242
    PHP/MySQL just gets data dynamically. You need to use some form of HTML and possibly CSS to pretify it:

    PHP Code:
    <?php
    while($result mysql_fetch_array($query)) {
    ?>
        <h1><?php echo $result['subject']; ?></h1>
        <h2><?php echo $result['name']; ?></h2>
        <p><?php echo $result['post']; ?></p>
        <p style="font-size:0.9em; font-style:italic;"><?php echo $result['expire']; ?></p>
    <?php
    }
    ?>
    Something like that

  5. Thanks to Hightower from:

    glennda (9th February 2011)

  6. #5

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,821
    Thank Post
    272
    Thanked 1,140 Times in 1,036 Posts
    Rep Power
    351
    Cheers.

    I was trying to do it all in the same script!

  7. #6

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242
    You can echo HTML in the PHP as follows:

    PHP Code:
    echo $result['something'] . " and static text or HTML <h1>HEADING</h1> can go here followed by more PHP " $result['somethingelse']; 
    But I find it so much easier to read by closing off the PHP tags and using little snippets within the HTML where needed. To echo PHP and static text in the same line just remember to use a '.' between the PHP element and the static. Also, static stuff has to be in quotes of some kind, either " or '

    EDIT: Just another tip. When formatting I tend to do this:

    <h1>HEADING HERE</h1>
    <h2>SUB HEADING HERE</h2>
    <p>BODY HERE</p>

    Then once I'm happy the static text looks how I want it to be I go and swap HEADING HERE with <?php echo $result['heading']; ?> and so on.

  8. #7

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242
    Oh, last bit too. Instead of doing:

    PHP Code:
    echo $result['one']; echo $result['two']; echo $result['three']; 
    you can just type

    PHP Code:
    echo $result['one'] . $result['two'] . $result['three']; 

  9. #8

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,821
    Thank Post
    272
    Thanked 1,140 Times in 1,036 Posts
    Rep Power
    351
    Cheers I will have a play with that.



SHARE:
+ Post New Thread

Similar Threads

  1. echo wireless mac address to a file
    By RabbieBurns in forum Scripts
    Replies: 8
    Last Post: 9th June 2010, 12:27 PM
  2. Results, how do you do them?
    By zag in forum MIS Systems
    Replies: 22
    Last Post: 17th November 2009, 11:34 PM
  3. Uniform 5 Echo Romeo Errors
    By jamesreedersmith in forum General Chat
    Replies: 6
    Last Post: 19th October 2009, 06:58 PM
  4. Got my exam results!
    By sparkeh in forum General Chat
    Replies: 13
    Last Post: 25th August 2009, 01:41 AM
  5. Results
    By ctbjs in forum ICT KS3 SATS Tests
    Replies: 21
    Last Post: 9th July 2007, 06:57 AM

Thread Information

Users Browsing this Thread

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

Posting Permissions

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