Jump to content

Difficulty understanding multidimensional form array


mykmallett

Recommended Posts

Hi, I have a form that I want to allow users to be able to add specific links, some are set types (domains) and just require the iser to enter the link, however, one is for whatever they wish and they must enter a name to describe it.

 

Now im going to enter it into a database with the following columns:

 

USER_ID || LINK_URL || LINK_TYPE || LINK_NAME

 

Link name is optional (only needed if the link type is their own custom one), the link types are ID's related to a table of link types (facebook, twitter etc).

 

Now I'm assuming that to differentiate the form elements for insertion into the database I would need to have a hidden form element with the type ID inside? However I don't really know how to go on from here.

 

 

A small version of the form would be

 

<input type="text" name="facebook_link" />

<input type="hidden" name="facebook_id value="1" />

 

<input type="text" name="twitter_link" />

<input type="hidden" name="twitter_id value="2" />

 

<input type="text" name="own_site" />

<input type="text" name="own_site_description" />

<input type="hidden name="own_site_id" value="3" />

 

 

However, I'm assuming this is wrong because I'll need to enter everything into an array won't I? I just can't get my head round it to out put thi results into a foreach statement.

 

 

Can anybody help me please?

Link to comment
Share on other sites

No need for hidden fields. Create your form fields AS arrays with the link ID as the array index. Something like this:

Facebook Link: <input type="text" name="links[1]" /><br>
Twitter Link: <input type="text" name="links[2]" /><br>
Custom Link: <input type="text" name="links[3]" /><br>
Link Name: <input type="text" name="link_name" />

 

Then in your PHP code you can loop through all the entries using

foreach($_POSTS['links'] as $linkID => $linkValue)
{
    //Do something with input
}

Link to comment
Share on other sites

Also, I want to advise against doing a separate INSERT query for each record - it is a very bad practice. Here is a more complete script for the processing logic

$userID = (int) $_SESSION['user_id'];

//Generate insert values for each record
$linkValues = array();
foreach($_POST['links'] as $linkID => $linkValue)
{
    //Parse input
    $linkID = (int) $linkID;
    $linkValue = mysql_real_escape_string(trim($linkValue));

    //Create insert record if value entered
    if(!empty($linkValue))
    {
        $linkName = ($linkID!=3) ? 'NULL' : mysql_real_escape_string(trim($_POST['link_name']));
        $linkValues[] = "($userID, '$linkValue', $linkID, '$linkName')";
    }
}

//Create and run ONE insert query for all entered values
$query = "INSERT INTO [table_name] (`USER_ID`, `LINK_URL`, `LINK_TYPE`, `LINK_NAME`)
          VALUES " . implode(', ', $linkValues);
$result = mysql_query($query) or die(mysql_error());

Link to comment
Share on other sites

Wow, fantastic! Thanks very much.

 

Just so I'm clear, as I'm unfamiliar with this syntax, this:

$linkName = ($linkID!=3) ? 'NULL' : mysql_real_escape_string(trim($_POST['link_name']));

 

Means if $linkID does not equal 3, then $linkName = 'NULL', else it equals the escaped value of $_POST['link_name']

 

Am I correct?

Link to comment
Share on other sites

...

 

Just so I'm clear, as I'm unfamiliar with this syntax, this:

$linkName = ($linkID!=3) ? 'NULL' : mysql_real_escape_string(trim($_POST['link_name']));

 

Means if $linkID does not equal 3, then $linkName = 'NULL', else it equals the escaped value of $_POST['link_name']

 

Am I correct?

 

Yes, that is called a ternary operator and acts as a quasi IF/ELSE statement. I use it most frequently when I need to assign one of two values to a variable based on a condition. But, it has other uses as well. Here is the basic structure:

 

([CONDITION]) ? [TRUE_RESULT] : [FALSE_RESULT];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.