Jump to content

using array's and explode in php


twome

Recommended Posts

I have an item that I get from the database, it's a string separated by commas. To get the individual pieces I use the explode command. I would like the array index to be the same value as the exploded pieces. How can I do that? I imagine I would have a for each loop? Any suggestions?

Here's what I have so far:

[code]
$query = "SELECT name, address, type FROM member m INNER JOIN profile p ON ";
$query .= "m.memberID = p.memberID WHERE m.memberID = '".$sessUserID."'";
        
$dbResults = mysql_query($query);
$result = mysql_fetch_array($dbResults);
        
$pieces = explode(",", $result['type']);
print_r ($pieces);
[/code]

[u]Output[/u]
Array ( [0] => 1 [1] => 5 [2] => 10 [3] => 12 )

[u]What I want the array to look like is this[/u]:
Array ( [1] => 1 [5] => 5 [10] => 10 [12] => 12 )

[u]Ultimately, I have a group of checkboxes on a form[/u]:
<input name="type[]" type="checkbox" value="1" />
<input name="type[]" type="checkbox" value="2" />
<input name="type[]" type="checkbox" value="3" />
........
<input name="type[]" type="checkbox" value="14" />

Pretty much, I need to match the values in the array with the checkboxes -- if there is a match then display that checkbox as "checked".

So according to the array results in the above example the checked check boxes would be:
<input name="type[]" type="checkbox" value="1" checked="checked" />
<input name="type[]" type="checkbox" value="5" checked="checked" />
<input name="type[]" type="checkbox" value="10" checked="checked" />
<input name="type[]" type="checkbox" value="12" checked="checked" />

Link to comment
Share on other sites

[code]
#i took this function of php.net/array_combine
#if you're using php5 you can just call array_combine
function array_combine_emulated( $keys, $vals ) {
$keys = array_values( (array) $keys );
$vals = array_values( (array) $vals );
$n = max( count( $keys ), count( $vals ) );
$r = array();
for( $i=0; $i<$n; $i++ ) {
  $r[ $keys[ $i ] ] = $vals[ $i ];
}
return $r;
}

$testArray = array("10","12","13","34","15");
$keys = array_values(($testArray));
$newArray = array_combine_emulated($keys,$testArray);
print_r($newArray); # prints  Array ( [10] => 10 [12] => 12 [13] => 13 [34] => 34 [15] => 15 )
[/code]
Link to comment
Share on other sites

try


[code]$query = "SELECT name, address, type FROM member m INNER JOIN profile p ON ";
$query .= "m.memberID = p.memberID WHERE m.memberID = '".$sessUserID."'";
        
$dbResults = mysql_query($query);
$result = mysql_fetch_array($dbResults);
        
$pieces = explode(",", $result['type']);

for ($i=1; $i <=14; $i++) {

     $chk = in_array($i, $pieces) ? 'checked' : '';
     echo "<input name='type[]' type='checkbox' value='$i' $chk /> $i<br>";
}[/code]

Setting the checkbox value to $i instead of '1' means you can now update the type column by

[code]$pieces = join (',', $_POST['type']);

$sql = "UPDATE member SET `type` = '$pieces' WHERE memberID = '$sessUserID' ";[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.