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.
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++;
}
}
$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'].
I have about 45 different values that I need to add, and to avoid having a really long line of variable + variable + variable, I'm looking for a way to add them vertically to make it easier to read.
Only thing I can think of is to load them into an array, but is there a better way?
$variable+
$variable+
$variable+
$variable+
Load them all into an array and then use array_sum
This is pretty dirty, but if you have a common variable name pattern, like the last 6-10 characters as you say. Then you could do this:
$postfixPatterns = array("xxxxxx", "yyyyyy");
$definedVars = get_defined_vars();
$sums = array_combine($postfixPatterns, array_fill(0, count($postfixPatterns), 0));
foreach ($definedVars as $cVarName => $cVar) {
foreach ($postfixPatterns as $cPattern) {
if (preg_match("/" . preg_quote($cPattern, "/") . "$/", $cVarName)) {
$sums[$cVarName] += $cVar;
}
}
}
// var_dump($sums); --> array('xxxxxx' => sum of xxxxxx,
// 'yyyyyy' => sum of yyyyyy);
Of course it would be better to refactor the code to store the values in an array in the first place!
You can also use this syntax :
$sum = $var1;
$sum += $var2;
$sum += $var3;
...
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
Might be an easy question for you guys. can't find it on google.
I am trying to concatenate two variables name;
$i=0;
for ($i=0;$i<5;$i++){
if($array[$i]>0){
$test.$i=//do something
}else{
$test.$i=//do something
}
}
//echo $test0 gives me nothing.
//echo $test1 gives me nothing.
I know I can't use $test.$i but don't know how to do this.Any helps? Thanks!
try ${$test.$i} = value
EDIT: http://php.net/manual/en/language.variables.variable.php
I'm assuming that the variables are called $test0, $test1, ..., $test5. You can use the following:
${"test".$i}
Though, might I suggest that you make $test an array and use $i as the index instead? It's very odd to use $i as an index to loop through a list of variable names.
As an example, instead of:
$test0 = "hello";
$test1 = "world";
Use:
$test[0] = "hello";
$test[1] = "world";
Try this:
for ($i=0;$i<5;$i++){
$the_test = $test.$i;
if($array[$i]>0){
$$the_test=//do something
}
else{
$$the_test=//do something
}
}
This might work:
$varName = $test . $i;
$$varName = ...
Can i ask where for this is neccesary?