Jump to content

Archived

This topic is now archived and is closed to further replies.

twome

using array's and explode in php

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" />

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.