Little bit of help with arrays...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Little bit of help with arrays...

Post by Skoalbasher »

Hi,

I'm working on a user creation page and i've got a few checkboxes I need to put into any array. I know how to make them an array, but have no idea how to put them into MySQL in an orderly fashion.

I was thinking about making a loop and throwing it all into one string and storing that. Then I could just parse the string and pull out what I need.

I also can't figure out how to put the data into a $_SESSION array. This is what I have at the moment.

registration.php

Code: Select all

 
  <tr>
      <td align="right">XBox&nbsp;</td>
      <td><input name="console[]" type="checkbox" id="xbox" value="xbox" /></td>
      <td><input name="genre[]" type="checkbox" id="mmorpg" value="mmorpg" />
      MMORPG</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td align="right">XBox 360&nbsp;</td>
      <td><input name="console[]" type="checkbox" id="xbox360" value="xbox360" /></td>
      <td><input name="genre[]" type="checkbox" id="rpg" value="rpg" />
      Single Player RPG </td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td align="right">PS2&nbsp;</td>
      <td><input name="console[]" type="checkbox" id="ps2" value="ps2" /></td>
      <td><input name="genre[]" type="checkbox" id="fps" value="fps" />
      FPS (First Person Shooter) </td>
      <td>&nbsp;</td>
    </tr>
 
handle.php (this is where you go when you click submit)

Code: Select all

 
$genre = $_POST['genre'];
 
if($genre)
{
    foreach($genre as $f) {
    echo $f."<br />";
    }
}else {}
 
If someone could give me a tip on how to turn that into a $_SESSION array, and how to make it easy to put and pull from MySQL, I'd appreciate it. The array I'm using for the rest of the user data is $_SESSION['user']['txtfieldhere'].
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Little bit of help with arrays...

Post by Christopher »

I would give an explanation and example code, but suprisingly the PHP manual has just that for both using sessions and MySQL:
Skoalbasher wrote:If someone could give me a tip on how to turn that into a $_SESSION array,
http://www.php.net/manual/en/ref.mysql.php
Skoalbasher wrote:and how to make it easy to put and pull from MySQL, I'd appreciate it.
http://www.php.net/manual/en/ref.session.php
(#10850)
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Little bit of help with arrays...

Post by Skoalbasher »

I know how to create an array in $_SESSION. What I'm not sure how to do is this.

Code: Select all

 
$genre = $_POST['genre'];
 
if($genre)
{
    foreach($genre as $f) {
    
    // Store data into session
    $_SESSION['user']['genre'] = $f;   //this will keep writing over the same array
 
    // Or should I do something like this? and parse the string back out when I want the data?
    $_SESSION['user']['genre'] = $_SESSION['user']['genre']."::".$f;
 
    // echo $f."<br />";
    }
 
}else {}
 
 
 
 
// Or, can I just do this?
 
$genre = $_POST['genre'];
if($genre)
{
     $_SESSION['user']['genre'] = $_POST['genre'];
}
 
// And if I can can I just put $_SESSION['user']['genre'] into one field in a MySQL INSERT query?
 
 
I usually find stuff on about.com or something, because the PHP examples are usually pretty advanced, or not what I want to do at all. But I can't see anything that gives me a better answer than what I have.

As for using MySQL, all I've found is information on how to put several rows of data into one array. I want to take several parts of a row and put it into an array without doing it manually.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Little bit of help with arrays...

Post by Christopher »

Code: Select all

if(isset($_POST['genre']))
{
    $_SESSION['user']['genre'] = $_POST['genre'];
    echo '<pre>' . print_r($_SESSION, 1) . '</pre>';
 
}
(#10850)
Post Reply