Retrieve Array Data through List/Menu box using Mysql query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TechnoAtif
    New Member
    • Sep 2007
    • 63

    Retrieve Array Data through List/Menu box using Mysql query

    Hi ALL

    I have entered some array values using checkboxes into mysql database through a form. Next iam creating a searchpage where all those cateogories inserted through checkboxes has to be retrieved using list/menu box.
    When i check only a single checkbox to insert the checked category ,selecting that category through list box gives out the entire data of the user corresponding to that category.
    However when i check multiple checkboxes and in the category select all those checked values to find if any user fits in all those category and hence prompt the entire data corresponding to it,the column shows only 'array' and not array values. So please anyone guide me as how should i fetch all the checked categories through the list/menu box.
    Here goes the edited code:


    Code to insert the array values in mysql database using checkboxes
    [PHP]
    <?
    $state=$_POST['state'];
    $state = implode(",", $state);

    $query="insert into consult (state)VALUES(' $state')";
    mysql_query($qu ery);
    ?>
    <form name="form" action="abc.php " method ="post">
    <input name=state[0] type="checkbox" value="Air Conditioning Consultants & Contractors">Ai r Conditioning Consultants & Contractors
    <input name=state[1] type="checkbox" value="Arbitrat ors" >Arbitrators
    <input name=state[2] type="checkbox" value=Builders, Contractors & Developers>Buil ders,Contractor s & Developers
    <input name=state[3] type="checkbox" value=Civil Engineers and Contactors>
    Civil Engineers and Contactors
    </form>
    <input name="submit" type="submit" class="style40" value="Submit">

    [/PHP]

    Code to fetch those checked array values using list/menu box.
    [PHP]
    <?php
    if (isset($_POST['submit']))

    {
    $state=$_POST['state'];
    $state=explode( ",",$state) ;
    }
    ?>
    <form name="form1" action="cons_sh ow.php" method="post" >
    <select name="state">
    <option selected >Select</option>
    <option value="Air Conditioning Consultants & Contractors">Ai r Conditioning Consultants & Contractors</option>
    <option value="Arbitrat ors">Arbitrator s</option>
    <option value="Builders ,Contractors & Developers">Bui lders,Contracto rs & Developers</option>
    <option value="Civil Engineers and Contractors">Ci vil Engineers and Contractors</option>
    </form>

    if(!isset($cmd) ) {

    $state=$_POST['state'];

    $query = @mysql_query("S ELECT * FROM consult where state='$state'" );

    if (!$query)
    {
    exit('<p>Error retrieving entries from database!<br/>'.'Error: ' . mysql_error() . '</p>');
    }

    $sql = mysql_fetch_arr ay($query);
    if($sql){

    echo '<table width="90%" cellspacing="2" cellpadding="4" border="1"><tr> <td valign="top">

    <table><tr><t d ><h4 ><u> Customer Information</u> </h4></td></tr></table>';

    echo '<table cellpadding="4" ><tr><td ><b>Firm Name</b></td><td ><b>State</b></td><td><b>City</b></td>

    <td><b>Contac t</b></td><td><b>Email Address</b></td></tr>';

    $firm = $sql['firmname'];

    $address = $sql['adress'];

    $city= $sql['city'];

    $pin=$sql['pin'];

    $tel=$sql['tel'];

    $fax=$sql['fax'];

    $email=$sql['email'];

    $state=$sql['state'];

    echo "<tr><td><a href='cons_deta ils.php?cmd=edi t&STATE=$state' >$firm</a></td><td>$state</td><td>$city</td><td>$tel</td><td>$email</td>";

    echo "<td>";
    echo "</tr></table>";

    }
    else {
    echo '<table ><tr><td>';


    [/PHP]

    This is how i have worked.but it is only useful for only a single checked data and cease to work on multiple checked data.
    So if anyone ca help please do it.

    Thanks and Regards
    TechnoAtif
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Where is variable '$cmd' set or assigned a value? Not knowing that makes it hard to follow your code. Statement
    [php]$query="insert into consult (state)VALUES(' $state')";[/php]
    makes field state a string of states separated by comma's.

    Somewhere in the 2nd piece of code you make $state an array. [php] $state=explode( ",",$state) ;[/php]But you cannot compare a single value field state with an array $state.

    So I suspect we have not seen al your code. Please show it and this time within the appropriate code tags!

    Ronald

    Comment

    • TechnoAtif
      New Member
      • Sep 2007
      • 63

      #3
      Hi

      Here is the page where cmd value has been posted.Basicall y it shows the checked values from the chexkboxes...bu t does not shows the selected values from the select box as i navigate to this page through the link given in the previous page. Hopt this could help you to give me sm able solution

      [PHP]<?php include "dbconnect.php" ;


      if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
      {
      if (!isset($_POST["submit"]))
      {
      $city = $_GET["CITY"];

      $sql = "SELECT * FROM consult where state'$state' ";
      $result = mysql_query($sq l);
      $query = mysql_fetch_arr ay($result);

      ?> [/PHP]

      [HTML]<form name="form" action="cons_de tails.php" >

      <table width="86%">
      <tr><td colspan="4" class="style38" ><b>Status (Tick as many as applicable):</b></td>
      </tr>
      <tr><td><?php echo "$query[state]<br/>" ; ?></td>
      </tr> </table>
      </form>[/HTML]

      Thanks and Regards
      Mohammad Atif

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        You still have not shown all code. E.g. the next piece of code is odd. Upon entry you get data from field 'state' in the $_POST array and 10 lines down you show a form asking for checkboxes that fille the 'state' array. This cannot work like you have shown it.[php]<?
        $state=$_POST['state'];
        $state = implode(",", $state);

        $query="insert into consult (state)VALUES(' $state')";
        mysql_query($qu ery);
        ?>
        <form name="form" action="abc.php " method ="post">
        <input name=state[0] type="checkbox" value="Air Conditioning Consultants & Contractors">Ai r Conditioning Consultants & Contractors
        <input name=state[1] type="checkbox" value="Arbitrat ors" >Arbitrators
        <input name=state[2] type="checkbox" value=Builders, Contractors & Developers>Buil ders,Contractor s & Developers
        <input name=state[3] type="checkbox" value=Civil Engineers and Contactors>
        Civil Engineers and Contactors
        </form>
        <input name="submit" type="submit" class="style40" value="Submit">
        [/php]I am sure there is a mixup of all those various pieces of code and it cannot be seen how they are working together. So show us all; the code if you want a sensible answer.

        You have not replied to my remark about using the state array as a single value.

        Ronald

        Comment

        • TechnoAtif
          New Member
          • Sep 2007
          • 63

          #5
          Ok..Sorry..

          Coding for the first two pages are same ,next i'm giving the coding for next two pages:

          In this third page the data should be shown on the click of the submit buttton of

          the previous page as written above:

          Third page:
          ===========
          [PHP]<?php

          include "dbconnect.php" ;
          //error_reporting ('notice');

          if(!isset($cmd) ) {


          $state=$_POST['state'];


          $query = @mysql_query("S ELECT * FROM consult where state='$state'" );

          if (!$query)
          {
          exit('<p>Error retrieving entries from database!<br/>'.'Error: ' . mysql_error() . '</p>');
          }

          $sql = mysql_fetch_arr ay($query);

          if($sql){

          echo '<table width="90%" cellspacing="2" cellpadding="4" border="1"><tr> <td valign="top">

          <table><tr><t d ><h4 ><u> Customer Information</u> </h4></td></tr></table>';

          echo '<table cellpadding="4" ><tr><td ><b>Firm Name</b></td><td ><b>Category</b></td><td><b>City</b></td>

          <td><b>Contac t</b></td><td><b>Email Address</b></td></tr>';


          //while ($sql) {



          $cid = $sql['cid'];

          $firm = $sql['firmname'];

          $address = $sql['adress'];

          $city= $sql['city'];

          $pin=$sql['pin'];

          $tel=$sql['tel'];

          $fax=$sql['fax'];

          $email=$sql['email'];

          $pass=$sql['pass'];

          $state=$sql['state'];

          $prj1=$sql['prj1'];

          $prj2=$sql['prj2'];

          $prj3=$sql['prj3'];

          $cprj1=$sql['cprj1'];

          $cprj2=$sql['cprj2'];

          $cprj3=$sql['cprj3'];

          $award=$sql['award'];

          $keyperson=$sql['keperson'];

          $des=$sql['design'];

          $time=$sql['time'];

          echo "<tr><td><a href='cons_deta ils.php?cmd=edi t&STATE=$state' >$firm</a></td><td>$state</td><td>$city</td><td>$tel</td><td>$email</td>";

          echo "<td>";



          echo "</tr></table>";

          }
          else {
          echo '<table ><tr><td>';

          echo "<font class=font>

          <pre>

          The database corresponding to your choice of city is

          currently unavailable.Ple ase try after some time to

          get the better results.

          Thanks</font>

          </pre>";

          echo '</td></tr></table>';
          }

          }

          ?>[/PHP]

          =============== =============== =============== =========

          This last page will show the data of a particular firm after clicking on the link below the firmname:



          [PHP]<?php include "dbconnect.php" ;
          error_reporting ('notice');

          if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
          {
          if (!isset($_POST["submit"]))
          {
          $state = $_GET["state"];

          $sql = "SELECT * FROM consult where state='$state' ";
          $result = mysql_query($sq l);
          $query = mysql_fetch_arr ay($result);
          //redirect("proto show.php");
          //exit();
          ?> [/PHP]
          [HTML]<form name="form" action="cons_de tails.php" method="post">
          <h3><center class="style78" >FOR ASSOCIATES/CONSULTANTS
          </center></h3>
          <table width="400" >
          <!--<tr>
          <td width="22%" class="style40" >< ?php echo $query['cid'];?></td>
          </tr>
          -->
          <tr>
          <td width="45%" class="style38" >
          <b>Name of Firm:</b>
          </td>
          <td width="11%" class="style40" ><?php echo $query['firmname'];?></td>
          </tr>
          <tr>
          <td class="style38" ><strong>Addres s</strong></td>
          <td class="style40" ><?php echo $query['adress'];?></td>
          </tr>

          <tr>
          <td class="style38" ><strong>City :</strong></td>
          <td class="style40" ><?php echo $query['city'];?></td>
          <td width="27%" class="style38" ><strong>Pin: </strong></td>
          <td width="17%" class="style40" ><?php echo $query['pin'];?></td>
          </tr>
          <tr>
          <td class="style38" ><strong>Tel: </strong></td>
          <td class="style40" ><?php echo $query['tel'];?></td>
          <td class="style38" ><strong>Fax: </strong></td>
          <td class="style40" ><?php echo $query['fax'];?></td>
          </tr>
          <tr><td class="style38" ><strong>E-mail:</strong></td>
          <td class="style40" ><?php echo $query['email'];?></td>
          </tr>
          </table>
          <table width="86%">
          <tr><td colspan="4" class="style38" ><b>Status (Tick as many as applicable):</b></td>
          </tr>
          <tr><td><?php echo "$query[state]<br/>" ; ?></td>
          </tr> </table>

          </form>[/HTML]
          [PHP]<?php }} ?>[/PHP]

          Hope this gives u some clue

          Somewhere in the 2nd piece of code you make $state an array.
          Code: ( php )
          $state=explode( ",",$state) ;
          But you cannot compare a single value field state with an array $state.
          I cannot get you...can u please elaborate

          Comment

          Working...