Shanna Chambers Shanna Chambers - 6 months ago 19
MySQL Question

passing two array in mysql different sizes one row

$str = implode(',',$name);
function trim_value(&$value)
$value = trim($value);

$Array = explode(',', $str);

array_walk($myArray, 'trim_value');

for($x=0; $x<count($myArray); $x++){

var_dump($Array[$x], $sports[$x]);
//this is just a portion of the code this is the part that's causing the problem

<input name="name[]"/><!--John Doe, Jane Doe-->
<input name="sports[]"/><!--tennis-->

<input name="name[]"/><!--William Doe-->
<input name="sports[]"/><!--football-->

suppose i want to pass name which is comma seprated names in the mysql database but i would like to implode then explode to split up the strings in an array to store then in there own respective rows. How would one write the php to do so using for loop.

so in the database the rows would look like row 1=John Doe| tennis |, row 2=Jane doe |tennis |, row 3=William Doe |football|. Inputs are generated dynamically using jquery. i have written a code to that addmore fields dynamically upon button click.

Tried doing it but i keep getting the first sports which is tennis applied to all the rows i want them to link just as i have stated. HELP!! HELP!! HELP!!


The fetched records need to be customized into a multi dimensional array such that one sport can have multiple sportsmen.

$rows = array(
  "John Doe| tennis |",
  "Jane doe |tennis |", 
  "William Doe |football|"

foreach($rows as $row) { 
    $fields = array_filter(explode("|", $row));   // Breaking up when | appears. Filtering is preferred because the last entry can return blank sometimes.
    $sportmen[trim($fields[1])][] = $fields[0];  // $fields[1] contain the sport. And $field[0] contain the player name.

So far, the $sportsmen array looks like this:

    [tennis] => Array
        [0] => John Doe
        [1] => Jane doe 

   [football] => Array
        [0] => William Doe 


Now, we simply need to loop through this array.

 <?php   foreach($sportmen as $sport => $players) { ?>
    <input name="name[]" value="<?php echo implode(",", $players); ?>" />
    <input name="sports" value="<?php echo $sport; ?>" />
 <?php } ?>