Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    New Coder
    Join Date
    Sep 2009
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question Generating link list from database? OOPS NOT RESOLVED

    I am extremely new to PHP and don't know where to begin with this, but it seems simple enough. I want to dynamically generate a list of links from a database. So for example I would have some sort of database or file on my website that was just a single column on numbers:

    125375
    193745
    058214

    Like that and it would insert the numbers into two links:

    1. site.com/link?id=125375 site.com/link2?id=125375
    2. site.com/link?id=193745 site.com/link2?id=193745
    3. site.com/link?id=058214 site.com/link2?id=058214

    Also, I want to format the output into an ordered list. I don't really know what I am doing, so any help is appreciated. Thanks.
    Last edited by Angrysafer; 09-09-2009 at 02:19 AM. Reason: Not Resolved LOL

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    First of all, you need your database setup like this:

    | ID | link |
    |---|-----|
    | 1 | 125375|

    etc


    Then, with your PHP, try this:

    PHP Code:
    <?php

    $query 
    "SELECT `link` FROM `TABLENAMEHERE`";
    $result mysql_query($query) or die(mysql_error());


    while(
    $row mysql_fetch_assoc($result))
    {
    ?>
    <a href="site.com/link2?id="<?php print $row['link']; ?>">LINK TEXT HERE</a>
    <
    <?php 
    }
    ?>
    I hope that helps a little, please post again with any questions.

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • Users who have thanked whizard for this post:

    Angrysafer (09-07-2009)

  • #3
    New Coder
    Join Date
    Sep 2009
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Wow, thanks! I wasn't expecting a response this quickly. I think I can use your code to do what I want, but I'm not experienced with databases either. I have cpanel access, and I just created a mySQL database, but all I can seem to do is add users, which I did and gave all privilleges to, but I don't see how to edit the database. Or am I doing something completely wrong?

    I hope it is ok to ask this here. I might need more help with the php side once I figure out my database.

  • #4
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Your cpanel should give you access to phpmyadmin - which will allow you to edit the database
    John

  • #5
    New Coder
    Join Date
    Sep 2009
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Alright, I created a table inside my database with a field with a single int value. Is this correct? In any case I can't seem to edit the actual database. Thank you for holding my hand through this, haha.
    Last edited by Angrysafer; 09-07-2009 at 05:42 PM.

  • #6
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    You want to set the ID row as the primary key, and AUTO_INCREMENT then simply add the linkid row.
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • Users who have thanked Zangeel for this post:

    Angrysafer (09-07-2009)

  • #7
    New Coder
    Join Date
    Sep 2009
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Ok, I made the changes to my table, but how do I add a row. I swear I am actually poking around in this thing on my own, but I'm not having much luck, and I am pretty intimidated. I already had to remake my table a few times because I think I messed something up.

  • #8
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    You should have one row in your table set like this:

    name: id
    type: int
    extra: auto_increment

    and another like this:

    name: link
    type: int


    That will work with the code I gave.



    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #9
    New Coder
    Join Date
    Sep 2009
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by whizard View Post
    You should have one row in your table set like this:

    name: id
    type: int
    extra: auto_increment

    and another like this:

    name: link
    type: int


    That will work with the code I gave.



    Dan
    Are rows the same thing as fields then?

    I've added two fields named id and link. I tried inserting one of my ids, I'm not really sure if it worked or not. I'm hopelessly confused.

    I've tried reading a few tutorials, but they all seem to have a slightly different phpMyAdmin version or something. I'm having trouble finding a tutorial for someone who has never looked at a database in their life.

    Also, I tried plugging in your php code into Amaya and it is giving me an error of "not well-formed (invalid token)" for this line:

    Code:
    <a href="site.com/link2?id="<?php print $row['link']; ?>">LINK TEXT HERE</a>
    It doesn't seem to like the second quotation mark. I'm not sure what the problem is.

  • #10
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Sorry, I made a couple mistakes.

    First, yes, by row I meant field. My bad.

    Second, the second quote shouldn't be there.
    It should be like this instead:
    <a href="site.com/link2?id=<?php print $row['link']; ?>">LINK TEXT HERE</a>

    OK, the ID is auto increment, which means that you don't have to put anything in the field, and the database will automatically insert the number in ID.

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #11
    New Coder
    Join Date
    Sep 2009
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Well Amaya is still giving an error, even with the code modified. Still no idea what's up with that. Sometimes those errors just go away. What should I be using to insert data into my database? Is there a query command I should be using, or is there an easier way, say importing a plain text file?

    Another thing that is bothering me is that your code doesn't seem to reference a database, just a table. Am I missing something?

    Haha, also Amaya doesn't seem to support php? I am figuring out notepad++ right now.
    Last edited by Angrysafer; 09-07-2009 at 09:21 PM.

  • #12
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Sorry, gotta go for class, but as far as referencing a db, good point, check out

    http://us2.php.net/manual/en/functio...-select-db.php

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #13
    New Coder
    Join Date
    Sep 2009
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I looked around in there, so do I need to create a user with say just view permissions to access the database but not put my user and password right into the php code?

    Also, this isn't a very complicated set of data, is there anyway to avoid mySQL entirely, it's giving me a real headache. Could I have the php just read through a text file, either with hard breaks, or comma separated, or something?

  • #14
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    Here's how you connect to your MySQL database, you do need to put your user and pass inside the code.

    PHP Code:
    <?php

    //Connect to MySQL Database
    $user 'your username goes here';
    $pass 'your password goes here';
    $db 'your database name goes here';

    mysql_connect(localhost$user$pass) or die(mysql_error());
    mysql_select_db($db) or die(mysql_error());


    echo 
    "<h1>We have successfully connected to the MySQL database now we can play around with our tables, e.g insert and retrieve data via queries</h1>";
    ?>
    Oh and of course you can do it by a file aswell, read here, you just simply read the file and the echo out the contents line by line.
    Last edited by Scriptet; 09-07-2009 at 10:26 PM.

  • Users who have thanked Scriptet for this post:

    Angrysafer (09-07-2009)

  • #15
    New Coder
    Join Date
    Sep 2009
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Scriptet, I just read the tutorial on that php site. I think this is how I want to proceed. It just seems simpler. I decided to make a few files to test things, but it didn't work out.

    I have phptest.php:
    Code:
    <html>
    <body>
    <?php
    $myFile = "links.txt";
    $fh = fopen($myFile, 'r');
    $theData = fgets($fh);
    fclose($fh);
    echo $theData;
    ?>
    </body>
    </html>
    and links.txt:
    Code:
    link1
    link2
    link3
    link4
    link5
    I uploaded both files to my server, but my output looks like this.

    Code:
    link1
    It is failing to read the rest of the lines. I didn't alter the code from his tutorial, so I am a little perplexed.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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