SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How Can I Pass or assign values from table to an array in PHP?

    Hello People,
    How can I put the inform loaded on a table on web form onto an array in PHP? I have an sql that is pulling information from database table on
    (html or PHP) page. When it is displayed in table (table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>). Now I want the records displayed on table page to be assigned or passed to an Array. How can I do It? The Table has a three columns as follows. Now I want these value sto be passed onto array in PHP

    No NameDesc CheckBox
    1 NorthSchool CheckBox
    2 Southchool CheckBox
    3 EastSchool CheckBox

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Most PHP database extensions contain functions for retrieving each row as an array. For example, mysql_fetch_assoc.

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think that you are not familiar with the function mysql_fetch_assoc() or mysql_fetch_array(). You can manipulate any type of records retrieved from the table in any way whether you want to store them in an array or display/print in the HTML table structure.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please I do not want to use mysql_fetch_assoc() or mysql_fetch_array() function. I want to use the TABLES AS ON HTML PAGE NOT DATABASE TABLE

    For example
    <form id="form1" method="post" action="">
    <table width="345" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td>1</td>
    <td> NorthSchool</td>
    <td><label>
    <input type="checkbox" name="checkbox" value="checkbox" />
    </label></td>
    </tr>
    <tr>
    <td>2</td>
    <td>Southchool</td>
    <td><label>
    <input type="checkbox" name="checkbox2" value="checkbox" />
    </label></td>
    </tr>
    <tr>
    <td>3</td>
    <td> EastSchool</td>
    <td><label>
    <input type="checkbox" name="checkbox3" value="checkbox" />
    </label></td>
    </tr>
    </table>
    </form>

  5. #5
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $_POST will have the submitted values. The html table is just a markup, php only receives the values submitted via form.

  6. #6
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want those values on the table to be passed on to an array or moved from one page to another as an array. The question is how do I get these values and passed them as an Array?

  7. #7
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've told you how. Use $_POST to access the values.

  8. #8
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Praetor,
    Kindly give me an example

  9. #9
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    $check1= $_POST["checkbox1"]; // checkbox1 is the name of the checkbox

  10. #10
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry we are talking different different things. I am mean an array For example
    <input type = 'checkbox' name = 'mychk[]'>
    <input type = 'checkbox' name = 'mychk[]'>


  11. #11
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $_POST['mychk'] will be an array. You would use it like any other array.
    PHP Code:
    print_r($_POST['mychk']); 
    You might want to supply a value attribute in the html though, so you know which checkbox was checked.

    Be aware, web browsers will not send a checkbox that is not "checked".

  12. #12
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello crmalibu,
    what do you mean by saying web browsers will not send a checkbox that is not "checked".

  13. #13
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by zamzam View Post
    Hello crmalibu,
    what do you mean by saying web browsers will not send a checkbox that is not "checked".
    Here, check (pun intended) it out.
    PHP Code:
    <?php
    if(true === array_key_exists('submit'$_POST))
    {
        echo 
    '<pre>'print_r($_POSTtrue);
        exit;
    }
    ?>
    <h4>Your favourite Beatles members.</h4>
    <form method="post" action="">
        <p>
            Ringo Starr
            <input type="checkbox" name="favBeatles[]" value="Ringo" />
        </p>
        <p>
            John Lennon
            <input type="checkbox" name="favBeatles[]" value="John" />
        </p>
        <p>
            Paul McCartney
            <input type="checkbox" name="favBeatles[]" value="Paul" />
        </p>
        <p>
            George Harrison
            <input type="checkbox" name="favBeatles[]" value="George" />
        </p>
        <input type="submit" name="submit" value="vote" />
    </form>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  14. #14
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks SilverBulletUK,
    but want I want is when the information reaches action php page those roles with check boxes are displayed. Have tried something below but it is not working. where I am going wrong?

    $state=$_POST['state'];
    foreach ($state as $statename)
    {
    $Checkstatus="$statename[checkbox]";
    If($Checkstatus=="C")
    {
    echo "$statename[Id]";

    }
    }


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
  •