www.webdeveloper.com
Results 1 to 12 of 12

Thread: PHP script to link an alphabetical list?

Hybrid View

  1. #1
    Join Date
    Oct 2013
    Posts
    16

    PHP script to link an alphabetical list?

    OK first thing is that i am new to all this, with that sad i have a working alphabetical search script where one clicks a letter such as "G" and the screen displays a list from my database with all the say 'lastNames' of a column that start with the letter "G".

    My problem is that after the list is displayed i would like for them to click on said 'lastName' ie "Goodman" and have it link you to another page such as a profile page where then it displays "Goodman"'s info from the database.

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    718
    So - when you build the "g" list, wrap the names in an <a> tag and just let the href of that call your search script with the name as a GET parameter.

    Your name list would look like (built from the results of a query using the PDO extension):

    Code:
    while ($row=$results->fetch(PDO::FETCH_ASSOC)
    {
    echo "<a href='myscript.php?sel_name={$row['lastNames']}'>{$row['lastNames']}</a>";
    echo "<br>";
    }
    and so on.

  3. #3
    Join Date
    Oct 2013
    Posts
    16
    Not sure what PDO extesion is or how to use it every thing im coding is in PHP and mysql using phpmyadmin.
    my code is based of this that i found online to use
    <?php
    $results = "";
    $letter = "";
    if(isset($_GET['letter']) && strlen($_GET['letter']) == 1){
    $letter = preg_replace('#[^a-z]#i', '', $_GET['letter']);
    if(strlen($letter) != 1){
    echo "ERROR: Hack Attempt, after filtration the variable is empty.";
    exit();
    }
    // Connect to database here now
    // SELECT * FROM movies WHERE title LIKE '%$letter'
    // Use a while loop to append database results into the $results variable ($results .=)
    // Close your database connection here after your while loop closes

    // The line below is only to use for testing purposes before you
    // attempt to connect to your database and query it, remove this line after initial test
    $results = "PHP recognizes the dynamic ".$letter." and can search MySQL using it";
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <script>
    var btns = "";
    var letters = "A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z";
    var letterArray = letters.split("");
    for(var i = 0; i < 26; i++){
    var letter = letterArray.shift();
    btns += '<button class="mybtns" onclick="alphabetSearch(\''+letter+'\');">'+letter+'</button>';
    }
    function alphabetSearch(let){
    window.location = "search_results.php?letter="+let;
    }
    </script>
    </head>
    <body>
    <script> document.write(btns); </script>
    <?php echo $results; ?>
    </body>
    </html

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    718
    Not sure what you mean by 'using phpadmin'. In your script above where you "say" 'connect to database...' you really have to have some php code there to call your db and execute a query. That's the part that I was referring to. And PDO is my choice of MySQL interfaces since eliminating the use of the php MySQL_* functions, due to their deprecation.

  5. #5
    Join Date
    Sep 2013
    Posts
    221
    Well...,you can populate dropdown list with values from database by the below example code:

    <select name="owner">
    <?php
    $sql = mysql_query("SELECT username FROM users");
    while ($row = mysql_fetch_array($sql)){
    echo "<option value=\"owner1\">" . $row['username'] . "</option>";
    }
    ?>
    </select>

    Hope this helps.
    strad solutionswww.stradsolutions.com

  6. #6
    Join Date
    Oct 2013
    Posts
    16
    Thanks for help finaly got it working but now for some reason i could not get the said profile page to display the requested data
    ive looked everywhere for help on it all the code i came across the sql = "select " statment is corect and the echo looks fine so not sure whats going caus i click on say "greenfield" the url shows the ?u=lastname but i get another name from the sql list

    any idea whats wrong

  7. #7
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    718
    Hard to debug your code if you don't show us the relevant parts.

  8. #8
    Join Date
    Oct 2013
    Posts
    16
    ok so dispite the fact that im a noob heres the code

    to start after i click on a specific letter like "A" my list shows up then i click on the a word like 'Absinthe' so then it links to
    http://localhost/Test_Site_1/index.php?u=Absinthe

    except insted of display the info for 'Absinthe' it displays my databases first row of info that has an id of 1 where 'Absinthe' has an id of (if that makes sense)

    so for the code to display any info is

    include_once("inc/mysql_connect.php");
    $u = "";
    $ing = "";
    $proc = "";

    $sql = "SELECT * FROM drinks ORDER BY ID LIMIT 1";
    $query = mysql_query($sql);
    while($row = mysql_fetch_array($query))
    {
    $u = $row["NAME"];
    $ing = $row['INGREDANT'];
    $proc = $row['PROC'];

    }

    so if anyone can help with displaying the proper info from data base i mean so that when i echo out $u i should get 'Absinthe' from the url of http://localhost/Test_Site_1/index.php?u=Absinthe and not an $row['NAME'] of say "#26" which is what shows up on screen

    i hope this makes sense

  9. #9
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    718
    Sorry - none of that made sense to me. Can't you just show the code and let it speak for you?

  10. #10
    Join Date
    Oct 2013
    Posts
    16
    index.php this is just my template for profile

    <?php
    include_once("inc/mysql_connect.php");
    $u = "";
    $ing = "";
    $proc = "";

    $sql = "SELECT * FROM drinks ORDER BY ID LIMIT 1";
    $query = mysql_query($sql);
    while($row = mysql_fetch_array($query))
    {
    $u = $row["NAME"];
    $ing = $row['INGREDANT'];
    $proc = $row['PROC'];

    }
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <link rel="stylesheet" type="text/css" href="style/main.css">


    </head>
    <body>

    <div id="header">bbbbccntent for New Div Tag Goes Here</div>
    <div id="body">
    <div class="bodyMiddle"><br /><br /><br /><br />
    <div id="name">

    <div id="name1"> <?php echo $u;?> </div>
    <div id="title">
    <h3>Ingredient:</h3>
    </div>

    <div id="ingred"><?php echo $ing; ?>

    </div>
    </div>


    <div id="image"><img src=img/Zombie alt="" width="171" height="209"> </div>

    <div id="proc"><br><br><br>
    <div id="prep">
    <h3>Preparation:</h3>
    </div><br><br>
    <div>
    <ol>
    <li>
    Pour the ingredients into a ****tail shaker with ice.</li>
    <li>Shake well.
    <li>Strain into a highball glass with crushed ice.</li>
    <li>Optionally, float the high-proof rum on top of the finished drink.
    </li>
    </ol></div>
    </div>
    </div></div>
    <div id="footer">Content for New Div Tag G</div>

    </body>
    </html>

    AlphaSearch.php this is for my Alphabetical search of the database

    <?php
    include_once("inc/mysql_connect.php");
    $results = "";
    $name = "";
    $letter = "";
    if(isset($_GET['letter']) && strlen($_GET['letter']) == 1){
    $letter = preg_replace('#[^a-z]#i', '', $_GET['letter']);
    if(strlen($letter) != 1){
    echo "ERROR: Hack Attempt, after filtration the variable is empty.";
    exit();
    }
    // Connect to database here now
    $sql = "SELECT * FROM drinks WHERE name LIKE '$letter%' ORDER by name asc";
    $results = mysql_query($sql);

    // Close your database connection here after your while loop closes

    // The line below is only to use for testing purposes before you
    // attempt to connect to your database and query it, remove this line after initial test

    }
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <link rel="stylesheet" type="text/css" href="style/main.css">

    <script>
    var btns = "";
    var letters = "A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z";
    var letterArray = letters.split("");
    for(var i = 0; i <101; i++){
    var letter = letterArray.shift();
    btns += '<a onclick="alphabetSearch(\''+letter+'\');">'+letter+'</a>';
    }
    function alphabetSearch(let){
    window.location = "AlphaSearch.php?letter="+let;
    }
    </script>
    </head>
    <body>

    <div id="header">cccccccntent for New Div Tag Goes Here</div>
    <div id="body">
    <div class="bodyMiddle"><br /><br /><br /><br />
    <div id="alpha"><br><br><br>
    <div id="alphalist" style=""><span style="text-align: center"></span>
    <h3><script> document.write(btns); </script></h3>
    </div>
    <div id="list"><?php while ($row = mysql_fetch_array($results))
    {
    $u = $row['NAME'];
    echo "<a href='index.php?u=$u'>$u</a>";
    echo "<br>";
    } ?> <br> </div><br>
    </div>
    </div>
    </div>
    <div id="footer">Content for New Div Tag G</div>

    </body>
    </html>

  11. #11
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    718
    1 - turn on error checking and display errors.
    2 - do your own error checking after operations such as your query execution.
    3 - are your field names spelled correctly and are they all REALLY in capitals? You retrieve the results of your queries using NAME, yet your query itself specifies 'name'. That can't be right.

  12. #12
    Join Date
    Oct 2013
    Posts
    16
    I am still having problems with the above code I would be very great full for some help on it.

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
  •  
HTML5 Development Center



Recent Articles