Jump to content


Photo

using array's and explode in php


  • Please log in to reply
2 replies to this topic

#1 twome

twome
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 25 April 2006 - 06:16 AM

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:

$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);

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

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

Ultimately, I have a group of checkboxes on a form:
<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" />



#2 KrisNz

KrisNz
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • LocationAuckland, New Zealand

Posted 25 April 2006 - 07:55 AM

#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 ) 


#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 25 April 2006 - 11:39 AM

try


$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>";
}

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

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

$sql = "UPDATE member SET `type` = '$pieces' WHERE memberID = '$sessUserID' ";

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users