Page 1 of 1

Arrays in single row/column?

Posted: Tue Apr 01, 2003 1:03 am
by shylock
I have been struggling with a design issue and I think I have come to the conclusion that you "can't do that with PHP/MySQL". Please help me confirm.

I have a site with 100+ products and would like to allow a salesperson to log in and be able to choose their region and then choose a set of products that they sell in that region from the entire product table, inserting their "set" (say 14 out of 125) into a row defined by their region/name/set.

I assumed for a while that I could load the whole product table of numbers/names into a select list form, have them choose their region/set and then have PHP insert that set (just ID numbers) into a field as a simple text list separated by spaces. Then I would be able to pull that field and use it as an array to then display their set in the future. HA.

First of all, I can't get the field to accept my insert record statement (it just puts the text "Array" in the field).
mysql_fetch_array only seems to work on multiple row query results. So, I may not be able to use that field even if it were populated.

Do I hear a round of "dohs"?

Any pointers would be appreciated. Other options for doing the same thing?

thanks

Posted: Tue Apr 01, 2003 2:18 am
by twigletmac
If you had:

Code: Select all

$myarray = array('a', 'b', 'c');
echo $myarray;
you would get as output:

Code: Select all

Array
If you show us the code you're having problems with then we will probably be able to help you out a bit more.

Mac

Posted: Tue Apr 01, 2003 2:16 pm
by shylock
OK. Looking a bit further into my books I found the explode function which I think is the solution to getting the list of values and making use of them.

I'm still not sure how to get the values "In" to the column.

Here's the basic multiple select list I'm using:

Code: Select all

<select name="prod_id&#1111;]" size="15" multiple id="prod_id&#1111;]">
      <?php
do &#123;  
?>
      <option value="<?php echo $row_rsProds&#1111;'products_id']?>"><?php echo $row_rsProds&#1111;'products_name']?></option>
      <?php
&#125; while ($row_rsProds = mysql_fetch_assoc($rsProds));
  $rows = mysql_num_rows($rsProds);
  if($rows > 0) &#123;
      mysql_data_seek($rsProds, 0);
	  $row_rsProds = mysql_fetch_assoc($rsProds);
  &#125;
?>
    </select>
Fairly basic

But, I can't seem to get the usual Insert function to accept the array created by the multiple-select as text string.
I guess I need to make a function to convert to a string from the array, then inser, yes?

Posted: Thu Apr 03, 2003 11:18 am
by shylock
OK. I figured it out.
There are actually 3 ways to do it, once you get your array from the select-multiple.

join($array)/unjoin($array)
implode($array)/explode($array)
serialize($array)/unserialize($array)

Serialize seems to be the best as it has built-in capability for addslashes

then MySQL is quite happy storing the array in a column. :P