$date_show1 = "01-".$date2;
$date = date($date)."-01";
$date_char = date($date);
$eee2 = mysqli_query($database->connection,"SELECT * FROM bon_info
WHERE date_day = '$date' AND creditcart != '8' AND creditcart != '6' AND
kassa_id = '$kassa_id'") or die(mysqli_error());
$num1 = mysqli_num_rows($eee2);
I tried the following script with a while loop but how can i also change the names of the variables also like
$date_show1 ..2 ..3 ..4 ..5 ..6 ..7 ..8 ..9 ..10 ..11 ..12
$num1 $num2 $num3......
I have no clue what you ask, apart from the numerating variables. You can use an array for that:
$variable[0] = 123;
$variable[1] = 123;
$variable[2] = 123;
$variable[3] = 123;
for($i=0;$i<=3; $i++){
echo $variable[$i];
}
Better usage for this would be a foreach. Imagine you unset value 2, the $i will echo a blank echo, because it doesn't exist. Waste over effort, therefor:
foreach($variable as $key=>$value){
echo $key.' = '.$value;
}
That method also supports non-digit keys (like ['name'] and ['id'])
You can concat a variable to make a variable name like that:
<?php
$my_date_var = "date_show1";
$my_date_var++; // date_show2
echo $$my_date_var; // if defined will echo variable $date_show2
Related
How I can use variable variables with array to get result like is below?
I've tried so far:
// $g_module_id_bar_1['id'] = 5;
$i = 1;
$variablename = 'g_module_id_bar_'.$i;
$key = '\'id\'';
echo $$variablename[$key];
Result should be: 5
You almost had it.
Change $key = '\'id\''; too $key='id';
The reason is because PHP understands that $key contains a string. When accessing an array noramlly, you wouldn't do something like:
<?php
$var = array("hello"=>"world");
echo $var["'hello'"];
which is effectively what you were doing
See for full solution:
https://3v4l.org/qk5ZL
You are trying to escape single quotes but this is useless, just use the string key:
$key = 'id';
echo $$variablename[$key]; // 5
Can someone explain me foreach loop in PHP
specifically in this example
<?php
$age = array("Peter"=>"35","Michel"=>"37","Finch"=>"43");
foreach($age as $x => x_values) // here I am getting confussion
{
echo "Key = ".$x."value = ".$x_values;
echo"<br>";
}
?>
You have not valid variable x_values, should be $x_values:
<?php
$age = array("Peter"=>"35","Michel"=>"37","Finch"=>"43");
foreach($age as $x => $x_values) // here I am getting confussion
{
echo "Key = ".$x."value = ".$x_values;
echo"<br>";
}
?>
foreach iterate through array, in your case it's $age. Variable $x get the keys from your array: Peter, Michel, Finch. Variable $x_values get the values: 35, 37, 43.
<?php
$age = array("Peter"=>"35","Michel"=>"37","Finch"=>"43");
foreach($age as $x => $x_values) // here I am getting confussion
{
echo "Key = ".$x."value = ".$x_values;
echo"<br>";
}
?>
$age= your array
$x = your array key
$x_values = your array value
echo "Key = ".$x."value = ".$x_values;
//ex: key = Peter Value = 35 (display like that)
Foreach takes the form of key => value so your array. Now the real problem you are having is syntax errors.
Like this foreach($age as $x => x_values) missing the $ for the x_values.
This is ok "Key = ".$x."value = ".$x_values; but we can simply do this "Key = $x value = $x_values"; instead. PHP will interpolate (interpret and replace) variables within double quotes. You can also do them this way "Key = {$x} value = {$x_values}"; Which saves a few characters over concatenation . and allows you to place a variable next to a word like this "$avalue" is seen as $avalue but "{$a}value" is seen as $a."value". Hope that makes sense.
That said, this '$a' is just the string $a because it's in single quotes.
$age = array("Peter"=>"35","Michel"=>"37","Finch"=>"43");
foreach($age as $x => $x_values) // here I am getting confussion
{
echo "Key = $x value = $x_values"; //fixed this
echo"<br>";
}
Output
Key = Peter value = 35
Key = Michel value = 37
Key = Finch value = 43
$view_array = implode(',', $view_array);
When I var_dump this the value is "3,1" how can I remove the outer quotes so it will become 3,1 only
The quotes are part of the way you are outputting it. They don't exist, there is no spoon. Use echo instead...
$view_array = [1,2,3];
$view_array = implode(',', $view_array);
echo $view_array;
Outputs
1,2,3
Well you can use explode, and just use the value by itself:
$view_array = explode(',', $view_array);
echo $view_array[0];
Or put it in a loop and render them all
$view_array = explode(',', $view_array);
for ($i=0; $i < count($view_array) ; $i++) {
# code...
echo $view_array[$i];
}
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++;
}
}
Say I want to echo an array but I want to make the value in the array I echo variable, how would I do this?
Below is kind of an explanation of what I won't to do but it isn't the correct syntax.
$number = 0;
echo myArray[$number];
I'm not sure what you mean. What you have isn't working because you're missing a $ in myArray:
$myArray = array('hi','hiya','hello','gday');
$index = 2;
echo $myArray[$index]; // prints hello
$index = 0;
echo $myArray[$index]; // prints hi
Unlike other languages, all PHP variable types are preceded by a dollar sign.
Just to add more. Another type of array is associative array, where the element is determined using some identifier, usually string.
$arrayStates = array('NY' => 'New York', 'CA' => 'California');
To display the values, you can use:
echo $arrayStates['NY']; //prints New York
or, you can also use its numeric index
echo $arrayStates[1]; //prints California
To iterate all values of an array, use foreach or for.
foreach($arrayStates as $state) {
echo $state;
}
Remember, if foreach is used on non-array, it will produce warning. So you may want to do:
if(is_array($arrayStates)) {
foreach($arrayStates as $state) {
echo $state;
}
}
Hope that helps!
You are nearly there:
$number = 0;
$myArray = array('a', 'b')
echo $myArray[$number]; // outputs 'a'
$myArray = array("one","two","three","four");
$arrSize=sizeof($myArray);
for ($number = 0; $number < $arrSize; $number++) {
echo "$myArray[$number] ";
}
// Output: one two three four
$myArray = array('hi','hiya','hello','gday');
for($count=0;$count<count($myArray);$count++)
{
$SingleValue = $myArray[$count];
$AllTogether = $AllTogether.",".$SingleValue;
}