$marks . $count = mysqli_real_escape_string($con, $_POST['marks$count']);
I want to use $count (variable) in ($con . $_POST[])
But as I tried it gave me error. I am very new for php coding.
You can't concatenate before assignment. The proper ways to use string variable concatenation:
$marks .= $count; //option 1
$newVar = $marks.$count; //option 2
$marks = $marks.$count; //option 3
$newVar = "$marks$count"; //option 4
The way you are attempting, there is no clear variable to make the assignment to. The value on the right side of the equal is being assigned to what's on the left side of the equal sign. Two variables on the left side is ambiguous.
You could do something like:
for($count=0;!empty($_POST['marks'][$count]);$count++){
$marks[$count] = mysqli_real_escape_string($con, $_POST['marks'][$count]);
}
At the end of the cycle, array $marks will contain all escaped values of array $_POST['marks'].
Related
With PHP I want to count the session variables $_SESSION key that start with a particular string.
eg:
FAVORITE-LISTING-04
FAVORITE-LISTING-24
FAVORITE-LISTING-58
with the above keys, count for "FAVORITE-LISTING-" will return: 3
Cheers
This should work for you:
<?php
session_start();
$_SESSION['FAVORITE-LISTING-04'] = "foo";
$_SESSION['FAVORITE-LISTING-24'] = "foo";
$_SESSION['FAVORITE-LISTING-58'] = "foo";
$count = substr_count(implode(array_keys($_SESSION)), "FAVORITE-LISTING-");
echo $count;
?>
Output:
3
You can make this working using a variable-variable which PHP does support. But I suggest instead to use a double array:
$_SESSION['FAVORITE-LISTING']['4'] = 'something';
$_SESSION['FAVORITE-LISTING']['24'] = 'something';
$_SESSION['FAVORITE-LISTING']['58'] = 'something';
count($_SESSION['FAVORITE-LISTING']);
That way you can retrieve data much easier and things keep organized.
Since $_SESSION is an array, just loop through it and look at the key's. Anytime a key begins with whatever you're string is you just add one more to the count. Since you're look for the beginning of the string, you want strpos() to equal 0 so you need to use === instead of ==.
$find = 'FAVORITE-LISTING-';
$count = 0;
foreach($_SESSION as $key => $value) {
if(strpos($key, $find) === 0) {
$count++;
}
}
I have variable of type varchar having values $pointmoney=356+2311;
pointmoney is a field in database from where i am retreiving this value .
now i have separte two variable $point and $money
where i want to store $point=356 and $money=2311 from $pointmoney=356+2311.(i.e separate values from '+').
If any 1 knows any similar function then pls answer.
hope i ellaborated well to understand my query . if anything is unclear pls feel to comment .
Use explode():
$pointmoney = "356+2311";
list($point, $money) = explode('+', $pointmoney);
you explode function like
$pointmoney = "356+2311";
$arr_money = explode("+",$pointmoney);
echo $point = $arr_money[0];
echo $money= $arr_money[1].
hope this will sure help you,
You can use explode() reference PHP explode
$pointAndmoney = "356+2311";
list($point,$money) = explode('+',$pointAndmoney); // now you have different variable for each
$arr = explode("+","54+99");
$point = $arr[0];
$money = $arr[1];
I have this question, and i can't find any radical answer...
So, is there any possibility to set two variable in one variable
if ($post) {
$'item_name'. $x .' = $_POST['item_name'. $x .''];
}
if x = 1 then,
$item_name1 = $_POST['item_name1'];
that's the bahivor I want to implement, a way to wright the first part of the post.
the main issue item_namex (x) could be 1, 2, 3, 4 ext
You are looking for variable variables in PHP:
$x = 7;
$var = 'item_name' . $x;
$$var = $_POST['item_name'. $x];
and then you get $item_name7 with value you want. Also trailing concatenation of .'' in your right side of assignment is useless
foreach($_POST as $key=>$value){
$$key=$value;
}
For every $key variable will be created and $value would be assigned to it
I know this is not the proper way to do this, however I am trying to put a quick fix on a form that was done by another developer. Basically I want to add an incremental number to a variable inside a while statement:
$count = 1;
while ($r = mysql_fetch_array($query)) {
$variable . $count = $r['somefield'];
$count++
}
So that makes the variables:
$variable1
$variable2
$variable3
....etc
$varname = 'variable' . $count;
$$varname = $r['somefield'];
http://www.php.net/manual/en/language.variables.variable.php
You'd be better off with an array...
$variable[] = $r['somefield'];
You can use variable variables, however it is probably not a good idea, especially for a trivial case like this one.
I have this small code , why it does not work and how to make it correctly ?
$temp = $_SESSION['contactPersonInterest'][$i];
$temp += ',Medlemskort';
//$_SESSION['contactPersonInterest'][$i] = $temp;
I am testing it with
?><script>alert('<?php echo $_SESSION['contactPersonInterest'][$i] ?>'+'----------'+'<?php echo $temp ?>');</script> <?php
And what i get is :
blbla,blll----------0
Whats wrong ?
Thank you
String concatenation is done with . in PHP. Try:
$temp .= ',Medlemskort';
Otherwise you perform addition, and if both strings don't start with numbers, they will be converted to 0 and 0 + 0 = 0 :)
Have a look at Type Juggling.
That's because += is an operator for adding integers, not strings. You want to concatenate strings (which is "."). Also, there is no need to create a temporary variable, only to overwrite the existing one. This should work:
$_SESSION['contactPersonInterest'][$i] .= ',Medlemskort';
You wrongly assign more things to the variable via +. You should use . instead.
$temp .= ',Medlemskort';
If you want $i to have temp's value, no need for the +=:
$temp = ""; // good habit to initialize before usage
$temp = $_SESSION['contactPersonInterest'][$i];
$temp = ',Medlemskort';
$_SESSION['contactPersonInterest'][$i] = $temp;
// or even save a $temp
$_SESSION['contactPersonInterest'][$i] = ',Medlemskort';
Hope this makes sense, good-luck