concatenating part of variable name with value of another

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
balcoder
Forum Newbie
Posts: 8
Joined: Thu Jun 24, 2010 8:52 am

concatenating part of variable name with value of another

Post by balcoder »

Hi
Trying to concatenate string '$num' with value in $j inside sql statement and keep getting a
truncated error on insertion.
Tried escaping With "\$num" but then the $value turns into a string instead of a variable. :banghead:
Any help would be greatly appreciated
Thanks

Code: Select all


  $num1 = 'F';
  $num2 ='T';
  $num3 = 'F';
  $num4 = 'F'; 
  foreach ($_POST['answer'] as $value)
  $j = 1;
  {
     
     $sql = "INSERT INTO answers SET
     answer = '$value',
     questionid = '$_POST[id]',
     truefalse = '$num'.$j";//Trying to concatenate string '$num' and value of $j so as to end up with $num1on first iteration $num2 on second and so on.
     $j++;
     if (!mysqli_query($link, $sql))
     {
				$error = 'Error inserting answer into answers '
        .mysqli_error($link);
				include 'error.html.php';
				exit();
     }
  }
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: concatenating part of variable name with value of anothe

Post by mayanktalwar1988 »

if its not getting to work inside do it outside
like this

Code: Select all

$i=num.$j;
now use $i in insert
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: concatenating part of variable name with value of anothe

Post by AbraCadaver »

Because you have only quoted $num and the dot inside the double quotes is seen as a dot and not concatenation operator. Try this:

Code: Select all

 $sql = "INSERT INTO answers SET
     answer = '$value',
     questionid = '$_POST[id]',
     truefalse = '$num$j'";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
balcoder
Forum Newbie
Posts: 8
Joined: Thu Jun 24, 2010 8:52 am

Re: concatenating part of variable name with value of anothe

Post by balcoder »

ok this is working a bit better but i still am getting the error Data truncated for column 'truefalse' at row 1
This is the table that I'm trying to insert the data into

Code: Select all

+------------+---------------+------+-----+---------+----------------+
| Field      | Type          | Null | Key | Default | Extra          |
+------------+---------------+------+-----+---------+----------------+
| id         | int(11)       | NO   | PRI | NULL    | auto_increment |
| answer     | text          | NO   |     | NULL    |                |
| truefalse  | enum('T','F') | NO   |     | NULL    |                |
| questionid | int(11)       | NO   |     | NULL    |                |
+------------+---------------+------+-----+---------+----------------+
As you can see the truefalse column is a enum set to T or F for true false

Code: Select all

foreach ($_POST['answer'] as $value)  
  {
     $x = "\$num".$j;
     $sql = "INSERT INTO mysqldev1answers set
     answer = '$value',
     questionid = '$_POST[id]',
     truefalse = '$x' ";
     $j++;
     if (!mysqli_query($link, $sql))
     {
				$error = 'Error inserting answer into dev1answers '
        .mysqli_error($link);
				include 'error.html.php';
				exit();
     }
}
If I dump out the results of the code below

Code: Select all

<?php
  echo "This is the number of elements in array answer $num_ans<br>";
  $j = 1;
  foreach ($_POST['answer'] as $value)  
  {
  $x = "\$num".$j;
  
   echo "$x<br>" ;
   $j++;
  
  }
I get this
This is the number of elements in array answer
$num1
$num2
$num3
$num4

Thanks for the help
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: concatenating part of variable name with value of anothe

Post by AbraCadaver »

I think I see what you're trying to do now. Variable variables work here, but that means that you'd be better off using an array. Show a print_r() of $_POST['answer'] and the form fields for answer.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
balcoder
Forum Newbie
Posts: 8
Joined: Thu Jun 24, 2010 8:52 am

Re: concatenating part of variable name with value of anothe

Post by balcoder »

AbraCadaver wrote:I think I see what you're trying to do now. Variable variables work here, but that means that you'd be better off using an array. Show a print_r() of $_POST['answer'] and the form fields for answer.
When i run this code

Code: Select all

print_r($_POST['answer']);
i get if the user has chosen 3 answers to their question the number of answers can vary between 2 and 4
Array ( [0] => One [1] => two [2] => Seven )

If I dump the array using print_r again but this time i have four answers to the question then i get this
Array ( [0] => nine [1] => two [2] => eight [3] => seven )

So that is why i need change the value passed in the sql insert every iteration
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: concatenating part of variable name with value of anothe

Post by AbraCadaver »

So what do those answers have to do with true or false?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
balcoder
Forum Newbie
Posts: 8
Joined: Thu Jun 24, 2010 8:52 am

Re: concatenating part of variable name with value of anothe

Post by balcoder »

AbraCadaver wrote:So what do those answers have to do with true or false?
The number of answers determine how many iterations the foreach loop will do.

For the line truefalse = '$num$j' I want this to be variable $num1 the first time round, $num2 the second, $num3 the third and so on.
So if the $_POST holds
Array ( [id] => 114 [answer] => Array ( [0] => nine [1] => two [2] => eight [3] => seven ) [quest] => Array ( [0] => 4 ) )
Then $num_true will equal 1 and the for loop will only run once, which will set $num4 = 'T'
The foreach loop will run 4 times and each times, each time I want the $sql variable to hold
INSERT INTO answers SET answer ='nine', questionid = '114', truefalse ='F'
INSERT INTO answers SET answer ='two', questionid = '114', truefalse ='F'
INSERT INTO answers SET answer ='eight', questionid = '114', truefalse ='F'
INSERT INTO answers SET answer ='seven, questionid = '114', truefalse ='T'

Code: Select all

$num_ans = count($_POST['answer']);
  $num_true = count($_POST['quest']);
  $num1 = 'F';
  $num2 ='F';
  $num3 = 'F';
  $num4 = 'F';
  
  for ($i=0;$i<$num_true;$i++)
  {
    if($_POST['quest']{$i} == 1)
    {
      $num1 = 'T';
    }
    elseif($_POST['quest']{$i} == 2)
    {
      $num2 = 'T';
    }
    elseif($_POST['quest']{$i} == 3)
    {
      $num3 = 'T';
    }
    elseif($_POST['quest']{$i} == 4)
    {
      $num4 = 'T';
    }

  }
  
  
  $j = 1;
  foreach ($_POST['answer'] as $value)  
  {
     //$x = "\$num".$j;
     $sql = "INSERT INTO answers set
     answer = '$value',
     questionid = '$_POST[id]',
     truefalse = '$num$j'"; //I want this to be variable $num1 the first time round, $num2 the second, $num3 the third and so on.
     $j++;
     if (!mysqli_query($link, $sql))
     {
				$error = 'Error inserting answer into answers '
        .mysqli_error($link);
				include 'error.html.php';
				exit();
		 }
		 
  }
  
I hope this makes things a little clearer
Thanks for the help
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: concatenating part of variable name with value of anothe

Post by AbraCadaver »

I'm sure there's a better way, but I'm still a little unsure why you're doing it this way. But here's one way to do it the way you have shown:

Code: Select all

$num = array('F', 'F', 'F', 'F');

foreach($_POST['quest'] as $quest)
{
	if($quest == 1)
	{
		$num[0] = 'T';
	}
	elseif($quest == 2)
	{
		$num[1] = 'T';
	}
	elseif($quest == 3)
	{
		$num[2] = 'T';
	}
	elseif($quest == 4)
	{
		$num[3] = 'T';
	}

}


$j = 0;
foreach ($_POST['answer'] as $value)  
{
	$sql = "INSERT INTO answers set
			answer = '$value',
			questionid = '$_POST[id]',
			truefalse = '$num[$j]'";
	
	$j++;
	if (!mysqli_query($link, $sql))
	{
		$error = 'Error inserting answer into answers '
				.mysqli_error($link);
		include 'error.html.php';
		exit();
	}
				
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
balcoder
Forum Newbie
Posts: 8
Joined: Thu Jun 24, 2010 8:52 am

Re: concatenating part of variable name with value of anothe

Post by balcoder »

AbraCadaver wrote:I'm sure there's a better way, but I'm still a little unsure why you're doing it this way. But here's one way to do it the way you have shown:

Code: Select all

$num = array('F', 'F', 'F', 'F');

foreach($_POST['quest'] as $quest)
{
	if($quest == 1)
	{
		$num[0] = 'T';
	}
	elseif($quest == 2)
	{
		$num[1] = 'T';
	}
	elseif($quest == 3)
	{
		$num[2] = 'T';
	}
	elseif($quest == 4)
	{
		$num[3] = 'T';
	}

}


$j = 0;
foreach ($_POST['answer'] as $value)  
{
	$sql = "INSERT INTO answers set
			answer = '$value',
			questionid = '$_POST[id]',
			truefalse = '$num[$j]'";
	
	$j++;
	if (!mysqli_query($link, $sql))
	{
		$error = 'Error inserting answer into answers '
				.mysqli_error($link);
		include 'error.html.php';
		exit();
	}
				
}
As you said . I'm sure there is a much better way to do it but thanks for solving that part.
It works great.
Thanks Abra
Post Reply