SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member
    Join Date
    May 2001
    Location
    United Kingom
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey all,

    well this is my first post and like a lot of people here, my first go at a php/mySQL site. The following code seems to be working but I wanted to know if it was right! Also there's a couple of questions if that's cool.

    Ok basically on my home page I have several news articles with links taking to you the news/index.php page where the article is displayed in full. eg homepage has the link http://www.djsource.co.uk/news/index.php?id=1

    so far the code I have on news/index.php to pull the info from my database is as follows, is this right??

    <?php
    $db = mysql_connect("localhost", "USERNAME", "PASSWORD");
    mysql_select_db("djsource_data", $db);
    $review = mysql_query("SELECT * FROM news ". "WHERE id=$id");
    while(list($id,$date,$title,$intro,$content,$mid,$pid,$aid) = mysql_fetch_row($review)) {
    echo "$date<br>$title<p>$intro<p>$content";
    }
    ?>

    so, that's the code, and like I say it seems to be displaying what I want it to but I need to add something else. I notice that if I just load news.php without giving it an id number (ie just news/index.php) then I get an error (not surprising).

    What I would like to do is add some code to the above so that when somebody hit's news.php without an article id number the page shows a list of just the titles of each news story ($title). Obviously they can then click the link of their interest.

    Phew, that's it. By thje way I've only been learning php/mySQL for 48hours so be gentle

    Thanks,
    Paul
    Last edited by djsource; May 23, 2001 at 12:43.
    http://www.djsource.co.uk - the complete dj source.

  2. #2
    SitePoint Evangelist ucahg's Avatar
    Join Date
    Apr 2001
    Location
    Sarnia, Ontario, Canada
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You spelt content wrong the second time

  3. #3
    SitePoint Member
    Join Date
    May 2001
    Location
    United Kingom
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    whoops, I made a typo when making the above post (I'll correct it now).

    Any advice on my atual coding problem?

    Paul.
    http://www.djsource.co.uk - the complete dj source.

  4. #4
    SitePoint Zealot Alarion's Avatar
    Join Date
    May 2001
    Location
    Virginia
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tried to view your site - I got a DNS error. new domain?

    Anyhow, you might try doing something like this:

    PHP Code:
    <?php 
    $db 
    mysql_connect("localhost""USERNAME""PASSWORD"); 
    mysql_select_db("djsource_data"$db); 
    if(isset(
    $id)) {
      
    $review mysql_query("SELECT * FROM news ""WHERE id=$id"); 
        
    // you don't need a while loop since you are only selecting one row from the database
        
    list($id,$date,$title,$intro,$content,$mid,$pid,$aid) = mysql_fetch_row($review);
         echo 
    "$date<br>$title<p>$intro</p><p>$content</p><br><br><font size=2>[ <a href=news.php>Back to news index</a> ]</font>"
        
    } else {
      
    $review mysql_query("SELECT id, date, title from news order by date");
      while(list(
    $id$date$title) = mysql_fetch_row($review)) {
        echo 
    "<a href=news.php?id=$id>$date - $title</a><br>";
    }
    ?>
    Sorry if this is a little late.. supervisor was talking my ear off
    Last edited by Alarion; May 23, 2001 at 12:44.
    -=Alarion=-
    Protollix - Linux hosting from $3.95/m

  5. #5
    SitePoint Member
    Join Date
    May 2001
    Location
    United Kingom
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Alarion
    Tried to view your site - I got a DNS error. new domain?
    No, just my bad memory again. The link to the test article should be working now. The other test page is http://www.djsource.co.uk/home.php.

    Nothings really working yet as I've only been at it for a couple of days. I have got the general design and feel for the new site done though

    Thanks for the code, I'll try it out now!!

    Paul
    http://www.djsource.co.uk - the complete dj source.

  6. #6
    SitePoint Zealot Alarion's Avatar
    Join Date
    May 2001
    Location
    Virginia
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nice looking site
    -=Alarion=-
    Protollix - Linux hosting from $3.95/m

  7. #7
    SitePoint Member
    Join Date
    May 2001
    Location
    United Kingom
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alarion -

    your code worked a treat, my page looks a bit messy bit a little tweaking should sort that out.

    Thanks again.
    Paul
    http://www.djsource.co.uk - the complete dj source.

  8. #8
    SitePoint Member
    Join Date
    May 2001
    Location
    United Kingom
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nice looking site
    Thanks, it's gonna be a cracker when this php/mysql version launches. The current version (http://www.djsource.co.uk) is a bit dark and gloomy, it's also just plain old html

    Another quick question for you then...

    once I've opened up the database, taken out the information and displayed it, don't I have to close the connection?

    I read through the tutorial here at site point and couldn't see anything about closing the connection, do you actually have to do this? what happens if you don't?

    thanks,
    Last edited by djsource; May 23, 2001 at 13:23.
    http://www.djsource.co.uk - the complete dj source.

  9. #9
    SitePoint Zealot
    Join Date
    Feb 2001
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What does this do?

    list($id,$date,$title,$intro,$content,$mid,$pid,$aid)

    Thanx!

  10. #10
    SitePoint Member
    Join Date
    May 2001
    Location
    United Kingom
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by d-net
    What does this do?

    list($id,$date,$title,$intro,$content,$mid,$pid,$aid)

    Thanx!
    Those are all the values/names of columns in the article table within my database.

    id - unique id number for each article
    date - date the article was published
    title - short text title
    intro - a paragraph of text used to entice people into reading.
    content - the main article
    mid - manufacturer id
    pid - product id
    aid - author id

    the last three names (mid,pid,aid) enable me to link the articles in this table to other tables and cross reference data. I haven't done this yet (still gatta learn) but I thought I'd set up the id columns now so I'm ready later on I've read here at site point that you should store each type of thing in a different table. In my case i'm storing news articles in one table, manufacturers in another, types of products in another and the author of the article in another. All I need to do now is get my head round all of this

    Paul.
    Last edited by djsource; May 23, 2001 at 13:52.
    http://www.djsource.co.uk - the complete dj source.

  11. #11
    SitePoint Zealot
    Join Date
    Feb 2001
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry if I misled you. I wanted to know what list() function does.

  12. #12
    SitePoint Zealot Alarion's Avatar
    Join Date
    May 2001
    Location
    Virginia
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    d-net
    quoted from http://www.php.net/manual/en/function.list.php
    list
    (unknown)

    list -- Assign variables as if they were an array
    Description

    void list (...)


    Like array(), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation.
    DJ
    Actually, no you don't *have* to. It's actually good programming practice to do it manually, but if you forget, PHP cleans up after you and closes the connection (unless you are using a persistent connection via mysql_pconnect())
    -=Alarion=-
    Protollix - Linux hosting from $3.95/m

  13. #13
    SitePoint Member
    Join Date
    May 2001
    Location
    United Kingom
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ha ha, did I get the wrong end of the stick or what

    I don't actually know what that does, I'm a bit of a cut and paste guy, save the understanding for another day. I'm sure it has the meaning over in the php manual at www.php.net

    Paul.
    Last edited by djsource; May 23, 2001 at 14:08.
    http://www.djsource.co.uk - the complete dj source.


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
  •