Help with php ...

Discussion in 'PHP' started by perik, Jan 21, 2010.

  1. #1
    Hi!

    I have a page built in php for different projects within culture and arts.
    In one part of the page the members of the page can presentate their projects.
    In this part I would like to have a userlist with the possibility to check which other
    members you have collaborated with in different projects like in the picture below.
    [​IMG]

    The problem is when I check several checkboxes there will still just show up one collaboration on the page.

    Heres an more detailed explanation:


    I get the this userlist with the code:
    
    // FROM        
            $projectmenu_selcatNY = '';
                if ($sql->db_Select('user', 'user_id, user_name', ' GROUP BY user_id', 'nowhere', false))
                {
                    while ($projectmenu_row = $sql->db_Fetch())
                    {
                        extract($projectmenu_row);
                        $projectmenu_selcatNY .= "$user_name<input type='checkbox' name='project_body' value='$user_name $user_id'<br ";
                        if ($user_id == $projectmenu_projectcat)
                        {
                        }
                        $projectmenu_selcatNY .= '>' . $tp->toFORM($projectmenu_bortmedborjan2[2], false) . '</option>';
                    } 
                }
                $projectmenu_selcatNY .= '</select>';
    // TO
    
    Code (markup):
    2) And this results in a page like below with lists of the collaborations that are checked in the list:
    [​IMG]

    With the code:
    SC_BEGIN project_VIEW_BODY
    global $tp,$user_id_project,$project_body,$user_id_project,$project_author,$projectmenu_postername,$user,$user_name,$projectmenu_postername;
    return "<a href='".e_BASE."user.php?id.$project_body'>$project_body</a>"; 
    SC_END
    Code (markup):
    Where project_VIEW_BODY is a shortcode for a templatefile. And $project_body is the value from the checkbox.

    Heres is also a picture of the database:
    [​IMG]

    MY QUESTION:
    The problem is when I check several checkboxes there will still just show up one collaboration on the page.

    What shall I change in the code to be able to make several users to get registrated in the database?

    So if I for example check:
    It ends up with a database that looks like:
    Thanks a lot for help!!!

    Perik
     
    perik, Jan 21, 2010 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The easiest way to deal with checkboxes that correspond to an array is to output them like this:

    <input type="checkbox" name="user[1]" value="1">Tamara Wan<br>
    <input type="checkbox" name="user[2]" value="1">David Peterson<br>
    <input type="checkbox" name="user[45]" value="1">Amy Ming
    HTML:
    Then, for example, assuming the boxes for Tamara and Amy but not David were checked, you'll get an array like this:

    
    $_REQUEST['user'] = array(1 => 1, 45 => 1)
    
    Code (markup):
    which is easy to process in a foreach loop.
     
    SmallPotatoes, Jan 21, 2010 IP
  3. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #3
    Avoid using $_REQUEST, it has huge security issues.
     
    Kaizoku, Jan 21, 2010 IP
  4. perik

    perik Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks for your reply

    ok I have now changed the code to:
    // FROM        
            $projectmenu_selcatNY = '';
                if ($sql->db_Select('user', 'user_id, user_name', ' GROUP BY user_id', 'nowhere', false))
                {
                    while ($projectmenu_row = $sql->db_Fetch())
                    {
                        extract($projectmenu_row);
                        [b]$projectmenu_selcatNY .= "$user_name<input type='checkbox' name='project_body[]' value='$user_name'<br ";[/b]
                        if ($user_id == $projectmenu_projectcat)
                        {
                        }
                        $projectmenu_selcatNY .= '>' . $tp->toFORM($projectmenu_bortmedborjan2[2], false) . '</option>';
                    }
                }
                $projectmenu_selcatNY .= '</select>';
    // TO
    Code (markup):
    when Im checking a name in the user list it will now registrate the word "Array" in the database instead.

    how do I write the code so it writes several names like:
    in the database instead of Array or overwriting the names?
    as you probably understand Im a newbie in php

    Thanks for your help!
     
    perik, Jan 22, 2010 IP
  5. perik

    perik Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i think the script where Im writing into the database is somewhere here?
            // New record so add it
            // check for duplicate
            if (!$sql->db_Select("projectmenu_projects", "project_id", "where project_name='" . $tp->toDB($_POST['project_name']) . "'", "", false))
            {
                $projectmenu_args = "'0',
    		'" . $tp->toDB($_POST['project_name']) . "',
    		'" . USERID . "." . USERNAME . "',
    		'" . $tp->toDB($_POST['project_servings']) . "',
    		'" . $tp->toDB($_POST['project_preptime']) . "',
    		'" . $tp->toDB($_POST['project_ingredients']) . "',
    		'" . $tp->toDB($_POST['project_body']) . "',
    		'" . $tp->toDB($_POST['project_source']) . "',
    		'" . $tp->toDB($_POST['project_nutrition']) . "',
    
    //
    // some code 
    // some code 
    // some code 
    //
    
        else
        {
            // Update existing
            $projectmenu_args = "
    		project_name='" . $tp->toDB($_POST['project_name']) . "',
    
    		project_servings='" . $tp->toDB($_POST['project_servings']) . "',
    		project_preptime='" . $tp->toDB($_POST['project_preptime']) . "',
    		project_ingredients='" . $tp->toDB($_POST['project_ingredients']) . "',
    		project_body='" . $tp->toDB($_POST['project_body']) . "',
    		project_source='" . $tp->toDB($_POST['project_source']) . "',
    		project_nutrition='" . $tp->toDB($_POST['project_nutrition']) . "',
    		project_category='" . intval($_POST['projectmenu_select']) . "',
    		project_approved='" . $project_approved . "',
    PHP:
    can that be? what shall I change?

    as it is Ive got this code in user_project.php:
    // FROM
    $projectmenu_selcatNY = '';
    if ($sql->db_Select('user', 'user_id, user_name', ' GROUP BY user_id', 'nowhere', false))
    {
    while ($projectmenu_row = $sql->db_Fetch())
    {
    extract($projectmenu_row);
    $projectmenu_selcatNY .= "$user_name<input type='checkbox' name='project_body[]' value='$user_name'<br ";
    if ($user_id == $projectmenu_projectcat)
    {
    }
    $projectmenu_selcatNY .= '>' . $tp->toFORM($projectmenu_bortmedborjan2[2], false) . '</option>';
    }
    }
    $projectmenu_selcatNY .= '</select>';
    // TO
    PHP:
    this code in an file for shortcodes called project_shortcodes.php
    SC_BEGIN project_VIEW_BODY
    global $tp,$user_id_project,$project_body,$user_id_project,$project_author,$projectmenu_postername,$user,$user_name,$projectmenu_postername;
    return "<a href='".e_BASE."user.php?id.$project_body'>$project_body</a>"; 
    SC_END
    PHP:

    and then this project_VIEW_BODY in an template file called project_template.php
        $project_VIEW_DETAIL .= '
    	<tr>
    		<td class="forumheader3" colspan="4"><br><b>' . RCPEMENU_A73 . '&nbsp;{project_VIEW_NAME}</b><br>{project_VIEW_INGREDIENTS}&nbsp;</td>
    	</tr>
    	<tr>
    		<td class="forumheader3" style="width:30%;vertical-align:top;">' . RCPEMENU_13 . '</td>
    		<td class="forumheader3" colspan="3">{project_VIEW_BODY}&nbsp;
    		<br>
    		{project_VIEW_NUTRITION}
    		</td>
    	</tr>';
    PHP:
     
    perik, Jan 22, 2010 IP
  6. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You can serialize the array, or turn it into a string by cross-referencing the keys with the values in your name array.

    Or, if you want the ability to search by which boxes were checked, you need to create a new table to hold the many-to-many relationships.

    You can google about this stuff or maybe someone with more patience can give you some sample code.
     
    SmallPotatoes, Jan 22, 2010 IP