...

View Full Version : splitting up array



SWI03
07-05-2005, 06:47 PM
I have a form that has checkboxes in it that i get through a query on a table the checkboxes are named my_events[] and I can insert them into my table fine with implode, and can even get them out of the table with this query



$my_results = MYSQL_QUERY("SELECT my_events FROM users WHERE username = '$username'");

echo "<ul>";
while($my_eventsp = MYSQL_FETCH_ARRAY($my_results)){

echo "<li>$my_eventsp[0]</li>";
}
echo "</ul>";


What I would like to do is seperate them to be listed seperatly, in the table they are seperated by a comma. I think I should be using explode() but haven't figured out how that would work with what I have now. Any help is appreciated.

Kid Charming
07-05-2005, 07:09 PM
I would advise you first to rethink your database design. Generally speaking, you shouldn't be storing comma-separated lists in your db; you should be storing each element of the list in its own field. Usually, this means a separate table.

Barring that, explode()'ll work fine. Explode $my_eventsp[0] separately and then loop through that array to add your list tags.

marek_mar
07-05-2005, 08:50 PM
Why not use serialize() (http://www.php.net/serialize)/unserialize() (http://www.php.net/inserialize)??

ClubCosmic
07-06-2005, 12:41 AM
while($row = MYSQL_FETCH_ARRAY($my_results)){

$my_eventsp = explode(",", $row[columnname]);

echo "<li>$my_eventsp[0]</li>";
}
echo "</ul>";

with mysql_fetch_array you can use the column name inside the brackets. $row[name] instead of $row[0]
my_eventsp[0] will give you the value of whats before the comma.

that might work,
good luck
c.c.

SWI03
07-06-2005, 01:00 AM
ok so I created a new table with these fields in it, username, name( which will be "my_events") and value.

now how would i go about inserting that array into the new table haven't figured it out and arrays are not really my thing.

Kid Charming
07-06-2005, 02:37 AM
I'd like to ask a couple of questions about your tables first, if I could. Does your users table have a unique id for each user aside from the username? In your new table, what kind of information is going to be in your value column?

SWI03
07-06-2005, 02:50 AM
yeah there is a uniqid in the main user table, but do not really need to have that in the table I just made. just a username will do because that information will not get displayed unless the user is logged in.

the value column is going to be the name of a category that they selected by checking a checkbox or multiple boxes. So what I was looking to do was just add the username to the usename field, then seeing I know what this is going to be for I can put my_events in the name field, then whatever checkboxes they selected then have an entry in the table for each one that was selected along with the username, my_events with it.

Or is there an easier way of doing it. Basically it will be building a profile for them to display information on their page that they can track.

Kid Charming
07-06-2005, 04:15 AM
You'd be better off using the unique id -- it'll make a more efficient index. Your other table doesn't need a my_events fields, since my_events should be the only type of info in it. So you've got two fields with userid and event (of course, you can name them anything you'd like; I'd avoid 'value' as too vague, though). Then you can do something like this for your loop:



$query = "SELECT event FROM my_events where userid = $id";
$result = mysql_query($query) or die(mysql_error());


echo "<ul>";

while( $row = mysql_fetch_row($result) )
{
echo "<li>$row[0]</li>";
}
echo "</ul>";



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum