SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    HELP PLZ! new to php. Drop down SORT by

    ok i have a tag board, and i need to have a drop down select how to sort/order by.

    there is error reporting and i dont udnerstand it, i am over my head and i JUST WANT TO COMPLETE THIS PROJECT! lol. i have 2 drop downs in there, i just need one functional one. Please help me!
    i think im calling a wrong variable or something, the sort changes/refreshes page and it looks like its working, but its not.

    here is the code
    PHP Code:
    <?php   
    error_reporting
    (E_ALL);
     
    $selected = array();
     
    $orderby $_GET[orderby];
    if(!
    $orderby) { $orderby 'tag_date'; }
     
    if(
    $orderby == 'tag_date'
    {
        
    $orderby_query "order by tag_date";
    }
    else if(
    $orderby == 'tag_cid')
    {
        
    $orderby_query "order by tag_cid";
    }
    else if(
    $orderby == 'tag_name')
    {
        
    $orderby_query "order by tag_name";
    }
    else { unset(
    $orderby); }
     
    // If $orderby was valid set the selected sort option for the form.
     
    if($orderby)
    {
        
    $selected[$orderby] = 'selected';
    }
     
    // Now run your SQL query with the $orderby_query variable.  Ex:
     
    $query "select * from tagboard $orderby_query";
     
    // SQL code goes here..
     
    ?>

    <?

    define
    ('HOST'"xx");
    define('USER'"xx");
    define('PASS'"xxx");
    define('DB'"xxx");

    function 
    doTags()
    {
    $conx mysql_connect(HOSTUSERPASS) or die("Unable to connect to MySQL Server");
    mysql_select_db(DB) or die("Unable to select Database");

    $stSql "SELECT tag_name, tag_cid, tag_entry, tag_date FROM tagboard $orderby";
    $arrResult = @mysql_query($stSql);
    if (!
    $arrResult) { echo "Query Failed - Unable to Retrieve tags - ".mysql_error()."  Action Aborted"; exit(); }

    if (
    mysql_num_rows($arrResult) >= 1)
    {

    while (
    $arrRow mysql_fetch_assoc($arrResult))
    {
    ?>

    <table width="100%" height="47" border="0" align="center" bgcolor="#FFFFFF">
    <tr>
    <td width="30%" height="30"><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #666666">  [<? echo date("F j, Y, g:i a"$arrRow['tag_date']); ?>]  </span></td>
    <td width="15%"><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 12px; font-weight: bold; color: #000000"><? echo stripslashes($arrRow['tag_name']); ?></span></td>
    <td width="10%"><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #686868"><? echo stripslashes($arrRow['tag_cid']); ?></span></td>
    <td width="45%"><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #000000"><? echo nl2br(stripslashes($arrRow['tag_entry'])); ?></span></td>
    </tr>
    <tr>
      <td height="11" >  </td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    </table>
    <?
    }
    } else {
    ?>
    <p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">No entries to display</p>
    <?
    }
    }

    function 
    doBoard()
    {
    ?>
    <html>
    <head>
    <title>Control Center Tag board</title>
    <style>
    body {
    font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF; 
    }
    td {
    font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; color: #666666;
    }
    .inputform {
    BORDER-RIGHT: #efefef 1px solid; BORDER-TOP: #000000 1px solid; FONT: 11px Verdana; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM: #efefef 1px solid; BACKGROUND-COLOR: #efefef
    }
    .inputformsub {
    BORDER-RIGHT: 1px outset; PADDING-RIGHT: 2px; BORDER-TOP: 1px outset; PADDING-LEFT: 2px; PADDING-BOTTOM: 2px; FONT: 11px Verdana; BORDER-LEFT: 1px outset; PADDING-TOP: 2px; BORDER-BOTTOM: 1px outset; BACKGROUND-COLOR: #dedede
    }
    </style>
    </head>
    <body>
    Sort by
    <form method=get style="display: inline;" name='orderby_form'>
    <input type=hidden name='param1' value="<?php print $param1?>">
    <input type=hidden name='param2' value="<?php print $param2?>">
    <select name=orderby onChange="orderby_form.submit();">
    <option value='name' <?php print $selected[$orderby]; ?>>Name</option>
    <option value='price_asc' <?php print $selected[$orderby]; ?>>Price (Low - High)</option>
    <option value='price_desc' <?php print $selected[$orderby]; ?>>Price (High - Low)</option>
    </select>
    </form>
    <div><div align="center">
    <br>Sort by
    <form method=get style="display: inline;" name='orderby_form'>
    </form></td>
    </tr>

    </form><br>
    <form action="board.php?do=Insert" target="tag_box" method="POST">
    <table width="757" height="250" bgcolor="#FFFFFF" style="border: 1px solid #000000">
    <tr>
    <td width="501" height="150"><IFRAME name="tag_box" height="350" width="751" src="board.php?do=Tags" frameborder="0"></IFRAME></td>
    </tr>
    <tr>
    <td height="100" align="center"><br>
      <table width="326" border="0">
        <tr>
          <td width="39">Name:</td>
          <td width="251"><input type="text" name="__name" size="20" maxlength="55" class="inputform"></td>
          <td width="22">&nbsp;</td>
        </tr>
        <tr>
          <td>CID:</td>
          <td><input type="text" name="__cid" size="6" maxlength="255" class="inputform"></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Entry:</td>
          <td><textarea name="__entry" cols="35" rows="4" class="inputform"></textarea></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Sort by:</td>
          <td><a href="sort"><span style="display: inline;">
            <select name=orderby onChange="orderby_form.submit();">
              <option value='name' <?php print $selected[$orderby]; ?>>Name</option>
              <option value="cid">CID</option>
              <option value="date">Date</option>
            </select>
          </span></a></td>
          <td><input type="submit" value="Submit Entry" class="inputformsub"></td>
        </tr>
      </table>
      </td></tr></table></form></div>
    </body>
    </html>
    <?
    }

    function 
    doInsert($__name$__entry$__cid)
    {
    $conx mysql_connect(HOSTUSERPASS) or die("Unable to connect to MySQL Server");
    mysql_select_db(DB) or die("Unable to select Database");

    $__name addslashes($__name);
    $__url addslashes($__cid); 
    $__entry addslashes($__entry);

    if (empty(
    $__name))
    {
    ?>
    <p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">Name Field Is Required</p>
    <meta http-equiv="refresh" content="5;url='board.php?do=Tags'">
    <?
    } elseif (empty($__entry))
    {
    ?>
    <p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">Entry Field Is Required</p>
    <meta http-equiv="refresh" content="5;url='board.php?do=Tags'">
    <?
    } else {

    $stSql "INSERT INTO tagboard(tagId, tag_name, tag_cid, tag_entry, tag_date) values('', '$__name', '$__cid', '$__entry', ".time().")";
    $arrResult = @mysql_query($stSql);
    if (!
    $arrResult) { echo "Query Failed - Unable to Retrieve tags - ".mysql_error()."  Action Aborted"; exit(); }

    if (
    mysql_affected_rows() == 1)
    {
    ?>
    <meta http-equiv="refresh" content="3;url='board.php?do=Tags'">
    <?
    } else {
    echo 
    "Unable to Add Entry to Database";
    ?>
    <meta http-equiv="refresh" content="5;url='board.php?do=Tags'">
    <?
    }
    }


    $do = (isset($_GET['do']) ? $_GET['do'] : FALSE);

    switch (
    $do) {
    case 
    "Board":
    doBoard();
    break;
    case 
    "Tags":
    doTags();
    break;
    case 
    "Insert":
    doInsert($_POST['__name'], $_POST['__entry'], $_POST['__cid']);
    break;
    default:
    doBoard();
    }
    ?>

  2. #2
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    United States
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $_GET['orderby'] will never equal tag_name, tag_cid, or tag_date..

    In your <option value="..."> you have the values: name, date and cid.

    So $_GET['orderby'] will equal: name, date or cid, never tag_name, tag_cid etc.. Hint: echo $_GET['orderby']; would help you debug what is being passed. Echo and print_r() everything when debugging. Scripts do exactly what you tell them. Make sure the right values are being passed.

    So you have a couple of problems.
    1. You are checking for the wrong values from $_GET['orderby'].
    2. You are assigning every single option as selected.

    So #1 Change your php sorting check to correlate with those values:

    PHP Code:
    if($orderby == 'date') {
        
    $orderby_query "order by tag_date";
    }
    else if(
    $orderby == 'cid') {
        
    $orderby_query "order by tag_cid";
    }
    else if(
    $orderby == 'name') {
        
    $orderby_query "order by tag_name";
    }
    else {
        
    $orderby null;

    Now, it works, but we can write that code better. I would replace the whole top part with this:

    PHP Code:
    <?php

    error_reporting
    (E_ALL);

    // Allowed Order Bys, Map these keys to these field names
    $orderMap = array(
        
    'date'    => 'tag_date',
        
    'cid'     => 'tag_cid',
        
    'name'    => 'tag_name',
    );

    $orderBy 'date';    // Default Key

    if (array_key_exists($_GET['orderby'], $orderMap) {
        
    $orderBy $_GET['orderby'];
    }

    $query "select * from tagboard ORDER BY " $orderMap[$orderBy]; 
    /* No SQL injection is possible, because we verified it was in our array */
    I added a bunch of comments for you to understand what is happening. Now all we need to do with our options is this:

    PHP Code:
            <select name="orderby" onChange="orderby_form.submit();">
              <option value="name">Name</option>
              <option value="cid" <?php if ($orderBy == 'cid') { echo 'selected'; } ?>>CID</option>
              <option value="date" <?php if ($orderBy == 'date') { echo 'selected'; } ?>>Date</option>
            </select>
    Please note, I didn't do a check for the name option because it is the first in the list. If it is first, it will be selected by default if no other option is selected.

    Edit: I just noticed that your code is in functions.. you will need to pass the variables into the function for them to actually be assigned! Or you need to put the code into the function.. one or the other..

  3. #3
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm..it gave me an error: unexpected '{' on line 10.

    here is my code, if u can just edit it, and post back, thatd be abolsutely great..im new to php and dont want to mess it up, i just need it working and the be done with this. you have been an amazing help!

    code:
    PHP Code:
     <?php
     error_reporting
    (E_ALL);// Allowed Order Bys, Map these keys to these field names
     
    $orderMap = array(    'date'     => 'tag_date',
                       
    'cid'     => 'tag_cid',
                       
    'name'    => 'tag_name',
                       );
     
     
    $orderBy 'date';    // Default Key
     
     
    if (array_key_exists($_GET['orderby'], $orderMap) {
        
    $orderBy $_GET['orderby'];
    }
        
    $query "select * from tagboard ORDER BY " $orderMap[$orderBy]; /* No SQL injection is possible, because we verified it was in our array */
         
    if($orderby == 'date') {    $orderby_query "order by tag_date";}else if($orderby == 'cid') {    $orderby_query "order by tag_cid";}else if($orderby == 'name') {    $orderby_query "order by tag_name";}else {    $orderby null;} 
     
    // SQL code goes here..

    define('HOST'"xxx");
    define('USER'"xxx");
    define('PASS'"xx");
    define('DB'"xxx");

    function 
    doTags()
    {
    global 
    $orderby_query;
    $conx mysql_connect(HOSTUSERPASS) or die("Unable to connect to MySQL Server");
    mysql_select_db(DB) or die("Unable to select Database");

    $stSql "SELECT tag_name, tag_cid, tag_entry, tag_date FROM tagboard $orderby_query";
    $arrResult = @mysql_query($stSql);
    if (!
    $arrResult) { echo "Query Failed - Unable to Retrieve tags - ".mysql_error()."  Action Aborted"; exit(); }

    if (
    mysql_num_rows($arrResult) >= 1)
    {

    while (
    $arrRow mysql_fetch_assoc($arrResult))
    {
    ?>

    <table width="100%" height="47" border="0" align="center" bgcolor="#FFFFFF">
    <tr>
    <td width="30%" height="30"><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #666666">  [<? echo date("F j, Y, g:i a"$arrRow['tag_date']); ?>]  </span></td>
    <td width="15%"><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 12px; font-weight: bold; color: #000000"><? echo stripslashes($arrRow['tag_name']); ?></span></td>
    <td width="10%"><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #686868"><? echo stripslashes($arrRow['tag_cid']); ?></span></td>
    <td width="45%"><span style="font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #000000"><? echo nl2br(stripslashes($arrRow['tag_entry'])); ?></span></td>
    </tr>
    <tr>
      <td height="11" >  </td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    </table>
    <?
    }
    } else {
    ?>
    <p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">No entries to display</p>
    <?
    }
    }

    function 
    doBoard()
    {
    ?>
    <html>
    <head>
    <title>Control Center Tag board</title>
    <style>
    body {
    font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF; 
    }
    td {
    font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; color: #666666;
    }
    .inputform {
    BORDER-RIGHT: #efefef 1px solid; BORDER-TOP: #000000 1px solid; FONT: 11px Verdana; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM: #efefef 1px solid; BACKGROUND-COLOR: #efefef
    }
    .inputformsub {
    BORDER-RIGHT: 1px outset; PADDING-RIGHT: 2px; BORDER-TOP: 1px outset; PADDING-LEFT: 2px; PADDING-BOTTOM: 2px; FONT: 11px Verdana; BORDER-LEFT: 1px outset; PADDING-TOP: 2px; BORDER-BOTTOM: 1px outset; BACKGROUND-COLOR: #dedede
    }
    </style>
    </head>
    <body>
    <div><div align="center"><br>
    <form action="board.php?do=Insert" target="tag_box" method="POST">
    <table width="757" height="250" bgcolor="#FFFFFF" style="border: 1px solid #000000">
    <tr>
    <td width="501" height="150"><IFRAME name="tag_box" height="350" width="751" src="board.php?do=Tags" frameborder="0"></IFRAME></td>
    </tr>
    <tr>
    <td height="100" align="center"><br>
      <table width="326" border="0">
        <tr>
          <td width="39">Name:</td>
          <td width="251"><input type="text" name="__name" size="20" maxlength="55" class="inputform"></td>
          <td width="22">&nbsp;</td>
        </tr>
        <tr>
          <td>CID:</td>
          <td><input type="text" name="__cid" size="6" maxlength="255" class="inputform"></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Entry:</td>
          <td><textarea name="__entry" cols="35" rows="4" class="inputform"></textarea></td>
          <td><select name="orderby" onChange="orderby_form.submit();">          <option value="name">Name</option>          <option value="cid" <?php if ($orderBy == 'cid') { echo 'selected'; } ?>>CID</option>          <option value="date" <?php if ($orderBy == 'date') { echo 'selected'; } ?>>Date</option>        </select> </td>
        </tr>
        <tr>
          <td>Sort by:</td>
          <td><a href="sort"><span style="display: inline;">
            <select name=orderby onChange="orderby_form.submit();">
              <option value='tag_name' <?php print $selected[$orderby]; ?>>Name</option>
              <option value="tag_cid">CID</option>
              <option value="tag_date">Date</option>
            </select>
          </span></a></td>
          <td><input type="submit" value="Submit Entry" class="inputformsub"></td>
        </tr>
      </table>
      <br>
      <div id="cid" style="display: block"></div>
    <div id="date" style="display: none"></td></tr></table></form></div>
    </body>
    </html>
    <?
    }

    function 
    doInsert($__name$__entry$__cid)
    {
    $conx mysql_connect(HOSTUSERPASS) or die("Unable to connect to MySQL Server");
    mysql_select_db(DB) or die("Unable to select Database");

    $__name addslashes($__name);
    $__url addslashes($__cid); 
    $__entry addslashes($__entry);

    if (empty(
    $__name))
    {
    ?>
    <p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">Name Field Is Required</p>
    <meta http-equiv="refresh" content="5;url='board.php?do=Tags'">
    <?
    } elseif (empty($__entry))
    {
    ?>
    <p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">Entry Field Is Required</p>
    <meta http-equiv="refresh" content="5;url='board.php?do=Tags'">
    <?
    } else {

    $stSql "INSERT INTO tagboard(tagId, tag_name, tag_cid, tag_entry, tag_date) values('', '$__name', '$__cid', '$__entry', ".time().")";
    $arrResult = @mysql_query($stSql);
    if (!
    $arrResult) { echo "Query Failed - Unable to Retrieve tags - ".mysql_error()."  Action Aborted"; exit(); }

    if (
    mysql_affected_rows() == 1)
    {
    ?>
    <meta http-equiv="refresh" content="3;url='board.php?do=Tags'">
    <?
    } else {
    echo 
    "Unable to Add Entry to Database";
    ?>
    <meta http-equiv="refresh" content="5;url='board.php?do=Tags'">
    <?
    }
    }


    $do = (isset($_GET['do']) ? $_GET['do'] : FALSE);

    switch (
    $do) {
    case 
    "Board":
    doBoard();
    break;
    case 
    "Tags":
    doTags();
    break;
    case 
    "Insert":
    doInsert($_POST['__name'], $_POST['__entry'], $_POST['__cid']);
    break;
    default:
    doBoard();
    }
    ?>

  4. #4
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i am very new to php, i am lost. If you can just re-post my code with the editited version i can pay you back with graphic work lol. i am not a coder, by any means and im getting killed with this project.

    I just need a tagboard with drop down sort. thats it.

    thanks again my friend.

  5. #5
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    United States
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I forgot a ) in the if statement...

    PHP Code:
    if (array_key_exists($_GET['orderby'], $orderMap)) { 
    Delete:

    PHP Code:
         if($orderby == 'date') {    $orderby_query "order by tag_date";}else if($orderby == 'cid') {    $orderby_query "order by tag_cid";}else if($orderby == 'name') {    $orderby_query "order by tag_name";}else {    $orderby null;} 
    You don't need it anymore..

    Messing it up is how you learn.. you should definitely look into googling for some tutorials. You have a lot to learn. If you absolutely need me to finish it I could, but you won't learn anything if I do it.

  6. #6
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i completely understand where you're coming from. However, i am af ront end designer, not a coder. and this is my only php project. So if you could finish it. i Just need that tagboard script and a drop down menu to choose how to sort it. thats it. That would be absolutely amazing..

    youd be such a huge help, ive spent over 6-7 hours on this simple tagboard with sort drop down, i just need it done!

    recap:
    use tagboard script, and just add a drop down with the 'tag_date(default, newest at top), tag_cid, tag_name). tahts the drop down order.

    i could pay you in graphic work, or we can just charge this to the game, and call it guy code?

    thanks so much!
    really.
    jt

  7. #7
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    United States
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey no problem. I sent you a PM.


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
  •