SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Update issues

  1. #1
    SitePoint Guru
    Join Date
    Jun 2001
    Location
    Australia
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Update iussues

    Hi all, I'm hoping for some help with an update query.

    What I am trying to do is allow me to order which menus will come first, second etc etc...

    The script only updates 1 feild, where I need it to update all of the $lmorder feilds.

    Here is my code so far.
    PHP Code:
    <?php
    include("adminlogin.php");
    include(
    "../config.php");
    include(
    "header.php");

    if (
    $submit):

      
    $sql "UPDATE leftmenu SET lmorder='$lmorder' WHERE lmid=$lmid";
      if (
    mysql_query($sql)) {
        echo(
    "<p>Database Updated!</p>");
      } else {
        echo(
    "<p>Problem : " .
             
    mysql_error() . "</p>");
      }
      
    ?>
    <br>
    <font size=4 face=arial> <b>Thanks Mate ;)</b></font></p>
    <?php
      
    else: 
    ?>
    <table width="100%" border="0" cellspacing="0" cellpadding="5" align="center">
    <tr> 
    <td bgcolor="#E5E5E5"><font face="Arial" size="4"><b>Left Menu</b></font>
    </td></tr></table><br>
    <table width="100%" border="0" cellspacing="1" cellpadding="1"><tr><td>
    <div align="right"><a href="addleftmenu.php"><font size="2">
    <b><font face="Arial"><i>Add a menu</i></font></b></font></a></div></td></tr></table>

    <table width="100%" border="0" cellspacing="1" cellpadding="4" align="center"><tr> 
    <td width=12% bgcolor="#EFEFEF"><b><font size="2" face="Arial">Order</font></b></td>
    <td width=34% bgcolor="#EFEFEF"><b><font size="2" face="Arial">Menu title</font></b></td>
    <td width=38% bgcolor="#EFEFEF"><b><font size="2" face="Arial">Content</font></b></td>
    <td width=8% bgcolor="#EFEFEF"><b><font size="2" face="Arial"><center>Edit</center></font></b></td>
    <td width=9% bgcolor="#EFEFEF"><b><font size="2" face="Arial"><center>Delete</center></font></b></td></tr></table>
    <form action="<?=$PHP_SELF?>" method="post">
    <?php

      $editmenu
    =mysql_query("SELECT lmid, content, left_title, lmorder FROM leftmenu ORDER BY lmorder");
      if (!
    $editmenu) {
        echo(
    "<p>Error fetching menu items: " .
          
    mysql_error() . "</p>");
        exit();
      }

    while (
    $edit mysql_fetch_array($editmenu)) {
    $lmid   $edit["lmid"];
    $left_title $edit["left_title"];
    $content $edit["content"];
    $lmorder $edit["lmorder"];

    ?>
    <table width="100%" border="1" cellspacing="1" cellpadding="5" align="center">
      <tr>
        <td width="12%"><input type="text" name="lmorder" value="<?=$lmorder?>" size="3" maxlength="3"></td>
        <td width="34%"><font size="2" face="Arial"><?=$left_title?></font></td>
        <td width="38%"><font size="2" face="Arial"><?=$content?></font></td>
        <td width="7%"><center><a href="editleftmenu.php?lmid=<?=$lmid?>"><img src="../images/edit.gif" alt="Edit Menu" border="0"></a></center></td>
        <td width="9%"><center><a href="deletemenu.php?lmid=<?=$lmid?>"><img src="../images/del.gif" alt="Delete Menu" border="0"></a></center></td>
      </tr>
    </table>
        <input type="hidden" name="lmid" value="<?=$lmid?>">
    <?php
    }
    ?>
    <input type="submit" name="submit" value="SUBMIT">
    </form>

    <!-- END OF FORM  -->

    <?php 
    include("footer.php");
    endif; 
    ?>
    The Demo site is here
    http://www.boxxnet.com.au/xgate/admin/menus.php
    Login: Admin
    Password: Admin

    Any help would be great. Thanks.

  2. #2
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the new file:
    Code:
    <?php
    include("adminlogin.php");
    include("../config.php");
    include("header.php");
    
    if ($submit):
    
      while (list($lmid,$lmorder)=each($alllmorder)) {
        $sql = "UPDATE leftmenu SET lmorder='$lmorder' WHERE lmid=$lmid";
        if (!mysql_query($sql)) {
          echo("<p>Problem : " .
               mysql_error() . "</p>");
        }
      }
      echo("<p>Database Updated!</p>");
      ?>
    <br>
    <font size=4 face=arial> <b>Thanks Mate <img src="images/smilies/wink.gif" border="0" alt=""></b></font></p>
    <?php
      else: 
    ?>
    <table width="100%" border="0" cellspacing="0" cellpadding="5" align="center">
    <tr> 
    <td bgcolor="#E5E5E5"><font face="Arial" size="4"><b>Left Menu</b></font>
    </td></tr></table><br>
    <table width="100%" border="0" cellspacing="1" cellpadding="1"><tr><td>
    <div align="right"><a href="addleftmenu.php"><font size="2">
    <b><font face="Arial"><i>Add a menu</i></font></b></font></a></div></td></tr></table>
    
    <table width="100%" border="0" cellspacing="1" cellpadding="4" align="center"><tr> 
    <td width=12% bgcolor="#EFEFEF"><b><font size="2" face="Arial">Order</font></b></td>
    <td width=34% bgcolor="#EFEFEF"><b><font size="2" face="Arial">Menu title</font></b></td>
    <td width=38% bgcolor="#EFEFEF"><b><font size="2" face="Arial">Content</font></b></td>
    <td width=8% bgcolor="#EFEFEF"><b><font size="2" face="Arial"><center>Edit</center></font></b></td>
    <td width=9% bgcolor="#EFEFEF"><b><font size="2" face="Arial"><center>Delete</center></font></b></td></tr></table>
    <form action="<?=$PHP_SELF?>" method="post">
    <?php
    
      $editmenu=mysql_query("SELECT lmid, content, left_title, lmorder FROM leftmenu ORDER BY lmorder");
      if (!$editmenu) {
        echo("<p>Error fetching menu items: " .
          mysql_error() . "</p>");
        exit();
      }
    
    while ($edit = mysql_fetch_array($editmenu)) {
    $lmid   = $edit["lmid"];
    $left_title = $edit["left_title"];
    $content = $edit["content"];
    $lmorder = $edit["lmorder"];
    
    ?>
    <table width="100%" border="1" cellspacing="1" cellpadding="5" align="center">
      <tr>
        <td width="12%"><input type="text" name="alllmorder[<?=$lmid?>]" value="<?=$lmorder?>" size="3" maxlength="3"></td>
        <td width="34%"><font size="2" face="Arial"><?=$left_title?></font></td>
        <td width="38%"><font size="2" face="Arial"><?=$content?></font></td>
        <td width="7%"><center><a href="editleftmenu.php?lmid=<?=$lmid?>"><img src="../images/edit.gif" alt="Edit Menu" border="0"></a></center></td>
        <td width="9%"><center><a href="deletemenu.php?lmid=<?=$lmid?>"><img src="../images/del.gif" alt="Delete Menu" border="0"></a></center></td>
      </tr>
    </table>
        <input type="hidden" name="lmid" value="<?=$lmid?>">
    <?php
    }
    ?>
    <input type="submit" name="submit" value="SUBMIT">
    </form>
    
    <!-- END OF FORM  -->
    
    <?php 
    include("footer.php");
    endif; ?>
    things I changed are marked in red.

    You can see I turned the input box for $lmorder into an array, $alllmorder[].
    The I go through that array, listing the key as $lmid and the value as $lmorder, then update it in the database.
    Last edited by FireFly; Dec 22, 2001 at 08:23.

  3. #3
    SitePoint Guru
    Join Date
    Jun 2001
    Location
    Australia
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Firefly, Thanks for helping me out.

    As much as I want to copy and paste that code and continue with my script, I would prefer to understand the =each (AND) alllmorder[<?=$lmid?>] function. Any information on it?

  4. #4
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    list(), each().
    Basically what they do together, is allowing you to go through an array, and every time giving you the current key and value.
    For example:
    Code:
    $array=array("key1" => "val1", "key2" => "val2", "key3" => "val3", "key4" => "val4");
    while (list ($key, $val) = each ($array)) {
    	echo $key . " => " . $val . "\n";
    }
    /* will output:
    key1 => val1
    key2 => val2
    key3 => val3
    key4 => val4
    */
    So by making the input boxes part of the $alllmorder array, you get the ability to use that array later, so anytime you list=each you will get the $lmid from the key, and the new $lmorder from the value.

    Got it?

  5. #5
    SitePoint Guru
    Join Date
    Jun 2001
    Location
    Australia
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks mate, now I get it.

    Time to do some tests.


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
  •