LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2003, 09:03 PM   #1
mychl
Member
 
Registered: Jul 2001
Location: Earth
Posts: 164

Rep: Reputation: 30
Variable names that include variables....


Heres what I got.... PHP, mysql, apache

Input a number, line_items=x

Then for x times take in item_".$counter
counter++

Then for x times insert into table (item_name) values ($item??????);

How can I add another variable, say counter2 to the end of another variable description.

Input comes in as $item_1, $item_2, etc...

I need to be able to set something like $item_$counter2 to get the contents of $item_1 when $counter2=1.

Any ideas???
 
Old 09-22-2003, 11:08 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Why dont you just use an array?

$Array[$id] = $element;
 
Old 09-23-2003, 08:18 AM   #3
mychl
Member
 
Registered: Jul 2001
Location: Earth
Posts: 164

Original Poster
Rep: Reputation: 30
I tried, and still am, but I don't know if I can pass an array as a hidden input type in an html form.

Can I have an array of strings?
 
Old 09-23-2003, 09:52 AM   #4
mychl
Member
 
Registered: Jul 2001
Location: Earth
Posts: 164

Original Poster
Rep: Reputation: 30
Lets say I have 3 variables getting passed to next.php.

$item_1
$item_2

also I have a counter getting passed... $num_items=2

Then on next.php I have a for loop, that will set $temp_item=$item_(here I want the for counter to be appended to the variable name)

It should look like this

$temp_item=$item_1;
$temp_item=$item_2;

Thanks
 
Old 09-23-2003, 10:38 AM   #5
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
I see what you are trying to do now. I have some html/php code that does this, but unfortunatly it is at home. I'll look it up later and post it.
 
Old 09-23-2003, 10:41 AM   #6
mychl
Member
 
Registered: Jul 2001
Location: Earth
Posts: 164

Original Poster
Rep: Reputation: 30
Thanks, would really appreciate it. I've been working on this for 3 days now... I might be getting close, but can't tell until I nail it.
 
Old 09-24-2003, 12:10 AM   #7
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Ok. Here is how I do it. It seems a bit different than what you are asking, but here it goes anyway.

The HTML code below is likely not valid. I had to delete soooo much stuff, that I didn't care if the tags matched. Just using it to display the name= stuff.

HTML code:
Code:
<INPUT TYPE="hidden" name=Total value="37"">
<INPUT TYPE="hidden" name=PictureIDForPlace0 value="406""><table border="1" width="100%">

   ... deleted stuff ...
<input type="button" onClick="PermittedString0.value=move(this.form.Permitted0,this.form.NotPermitted0, this.form.Permitted0)" value="<<">
<input type="button" onClick="PermittedString0.value=move(this.form.NotPermitted0,this.form.Permitted0, this.form.Permitted0)" value=">>">

<select multiple size="7" name="NotPermitted0" style="width:100">

<option value="1">lq</option>
<option value="2">lq</option>
<option value="3">lq</option>
<option value="4">lq</option>
<option value="5">lq</option>
<option value="6">lq</option>
<option value="7">lq</option>
</select>
</td>
<td>
<select multiple size="7" name="Permitted0" style="width:100">
<option value="1">dude</option>
<option value="2">dude</option>
<option value="3">dude</option>
<option value="4">dude</option>
<option value="5">dude</option>
<option value="6">dude</option>
<option value="7">dude</option>
</select>
</td></tr></table></center>

<INPUT TYPE="hidden" name=PermittedString0 value="1:2:3:4:">    </td>

... All over again, until I hit the total of 37 ...

<INPUT TYPE="hidden" name=PictureIDForPlace1 value="407""><table border="1" width="100%">
PHP code:
I did not write the InputParse class, and I have no clue who did.
Code:
class InputParse
{
  function InputParse()
  {

  }

  function validateInputs()
  {
    global $HTTP_POST_VARS;
    global $HTTP_GET_VARS;
    $vars = count($HTTP_POST_VARS) > 0 ? $HTTP_POST_VARS :
              (count($HTTP_GET_VARS) > 0 ? $HTTP_GET_VARS : array() );
    $cleanvars = array();
    reset($vars);
    while( list($var, $value) = each($vars) )
    {
      $cleanvars[$var] = $this->validateInput($value);
    }
    return $cleanvars;
  }

  /* private: sanitise a single variable */
  function validateInput($var)
  {
    if (get_magic_quotes_gpc())
    {
      $var = stripslashes($var);
    }
    $var = strip_tags($var);
    $var = htmlspecialchars($var);
//    $var = str_replace("\n", " ", $var);
    $var = str_replace("\r", " ", $var);
    $var = trim($var);

    return $var;
  }
}

?>
Code:
function pictureSubmit()
{
   $input = new InputParse();
   $vars = $input->validateInputs();

// deleted stuff

   $Total = $vars[Total];

   for ( $i = 0; $i < $Total; $i++ )
   {
      $PictureID = $vars["PictureIDForPlace".$i];
      $PermittedString = $vars["PermittedString".$i];

   ... the rest ...
The code above does look a bit confusing. If you need more help I can clean it up a bit.
 
Old 09-24-2003, 07:13 AM   #8
mychl
Member
 
Registered: Jul 2001
Location: Earth
Posts: 164

Original Poster
Rep: Reputation: 30
Thanks crabboy, but look what I found......

$item_array=array();
for($j=0;$j<$line_items;$j++){
$temp_name="item_name$j";
$temp_desc="item_description_$j";
$temp_revenue="item_revenue_$j";
mysql_query("insert into invoice_items(invoice_number,line_item,item_description,item_revenue,item_shipped)
values ('$invoice_number','${$temp_name}','${$temp_desc}','${$temp_revenue}','')")
or die(mysql_error());
$count=$j+1;
echo "Line Item ".$count." for Invoice ".$invoice_number." has been added.";
echo "<blockquote>Item: ".${$temp_name}."<BR>";
echo "Description: ".${$temp_desc}."<BR>";
echo "Revenue: ".${$temp_revenue}."<BR></blockquote>";
}


Using {} arount a variable name lets you access the contents of that variable.... so
$temp_variable='Temporary Value';
${$temp_variable}=Temporary Value

I was so thrilled when I found this.... I had a script similar to what you posted almost done, then this popped up in documentation....

Thanks again for your help...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing variables to an include? Red Squirrel Linux - Software 4 09-23-2005 09:18 PM
edit path environment variable to include current working directory alb1954 Linux - Newbie 8 07-11-2004 11:26 PM
What are the names of the variables passed to konqueror's email client? BluePyre Linux - Software 0 07-04-2004 11:33 AM
variable names in your code (fun) titanium_geek Programming 12 12-22-2003 10:12 AM
Perl question re arrays/variable names Jon- Linux - Software 1 09-19-2003 10:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration