I have array that contain 7 string value.
I want to store it in different variable.
but i have 10 variable so i wish that, the variable not contain any array value that's value is zero.
<?php
$str = ("my", "name" , "ajay "," and "," i "," am ", " vageration " );
?>
i want to get this string value like
$a1=
$a1=my
$a3=name
$a4=ajay
$a5=and
$a6=i
$a7=am
$a8=vageration
$a9=0
$a10=0
i don`t know how can i print this value
I think I wont answer to your question, but I don't really understand what you want.
If you want to dynamically assign variables in php, I could invite you to do something like that:
<?php
$str = ("my", "name" , "ajay "," and "," i "," am ", " vageration " );
for ($i = 0; $i < 10; $i++)
${'a'.$i} = (isset ($str[$i]) ? $str[$i] : 0);
?>
In your case, this will create the following variables :
<?php
$a0 = "my";
$a1 = "name";
$a2 = "ajay ";
$a3 = " and ";
$a4 = " i ";
$a5 = " am ";
$a6 = " vageration ";
$a7 = 0;
$a8 = 0;
$a9 = 0;
?>
This line (${'a'.$i} = (isset ($str[$i]) ? $str[$i] : 0) uses the ternary operator, which means that the variable ${'a'.$i} will have $str[$i]'s value if such a value exists or 0 otherwise.
Not sure it helps. I'll edit or delete my message if necessary.
Related
this is my code :
<?php
$sss = "SELECT tanggal_awal FROM libur_nasional GROUP BY tanggal_awal ASC";
$qqq = mysqli_query($konek,$sss);
$arr = array();
while ( $tam = mysqli_fetch_array($qqq)) {
$date = $tam['tanggal_awal'];
$reformat_date = date("n-j-Y", strtotime($date));
$arr[] = $reformat_date;
}
$array_date = implode(",", $arr);
?>
> output : 8-9-2018,8-10-2018,8-17-2018
> output i want to is : "8-9-2018", "8-10-2018", "8-17-2018"
could someone help me to get the output i want, thank before.
You can make your code a bit shorter:
$sss = "SELECT tanggal_awal FROM libur_nasional GROUP BY tanggal_awal ASC";
$qqq = mysqli_query($konek,$sss);
while ( $tam = mysqli_fetch_array($qqq)) {
$arr[] = date("n-j-Y", strtotime($tam['tanggal_awal']));
}
$array_date = '"' . implode('", ', $arr) . '"';
In php you don't need to declare an array before pushing values to it.
Your $date variable is not needed as it's just a copy of the $tam['tanggal_awal'] value.
$reformat_date is also not needed as you can place it in the array directly.
The imploding is what is placed between the items, by having ", there you just need to add a " at each end of the string.
$arr=['8-9-2018','8-10-2018','8-17-2018'];
$array_date = '"'.implode('", "', $arr) . '"';
echo $array_date;
Output ---> "8-9-2018", "8-10-2018", "8-17-2018"
Try to implode your array like this.
How to do make a string in quotes from integer in better way in php?
$number = 1;
$number = "'" . $number . "'";
result is '1', which is good, but can it be done more nicely? I tried (string) $number but result was just 1 not '1'.
$num = 1221;
echo $numStr = "'$num'";
Results: '1221'
You can also try:
$num = 1221;
echo $numStr = "'$num'"; // output: '1221'
var_dump($strVal); // output: string(4) "1221"
I need to sum the result set values separated by "|" inside the loop eg. set of values 10|2, 6|2, 8|1 should result in 24|5.
here is my code:
<?php
$fromdate="2016-03-31";
$todate="2016-03-31";
$TAG="1";
$con = mysqli_connect("XXXXX","XX","XXX","XXX");
$query = mysqli_query($con, "CALL sp_Android_Online_Dashboard('$fromdate', '$todate','$TAG')") or die("Query fail: " . mysqli_error());
$Totfiles = 0;
$file_minutes = 0;
$Tot_minutes=0;
$Pending=0;
while(($row = mysqli_fetch_array($query)))
{
$Totfiles +=$row["Totfiles"];
$file_minutes +=$row["file_minutes"];
$Pending =str_replace(array("/"),"|",$row["Pending"]); //need to sum all the values separated by "|"
$Tot_minutes +=$row["Tot_minutes"];
}
$response["Details"]['Totfiles'] = $Totfiles;
$response["Details"]['file_minutes'] = $file_minutes;
$response["Details"]['Pending'] = $Pending;
$response["Details"]['Tot_minutes'] = $Tot_minutes;
echo json_encode($response);
?>
$row["Pending"] contains the values which are to be summed
result am getting now,
"Pending":"16|9"
"Pending":"11|3"
"Pending":"6|2"
my expected result,
"Pending":"33|14"
This is what you are aiming at i think, you make an array first containing 2 values, on each iteration through the loop you add the new values to them and at the end you can implode it into a string again
// Start with an array containing 0 twice
$Totalpending = [0,0];
while(($row = mysqli_fetch_array($query)))
{
// On each loop we add the left value to the first value in the array and the right value to the second value in the array
$tmp = explode("|", $row['Pending']);
$Totalpending[0] += $tmp[0];
$Totalpending[1] += $tmp[1];
$Totfiles +=$row["Totfiles"];
$file_minutes +=$row["file_minutes"];
$Pending =str_replace(array("/"),"|",$row["Pending"]); //need to sum all the values separated by "|"
$Tot_minutes +=$row["Tot_minutes"];
}
// if you want to format the values in the same way again, although an array is much easier to handle, but it's up to you.
$stringTotalpending = implode('|',$Totalpending);
the string value you want will then be in $stringTotalpending
So Firstly, we need to explode the values from $row["Pending"].
$arr = explode("|", $row["Pending"]);
Now use a loop to add those two numbers:
$temp = 0;
for($i = 0; $i < count($arr); $i++){
$temp += (int) $arr[$i];
}
Now the $temp would contain the result.
As simple as that.
And also as a side note, your code is vulnerable to SQL-Injection attacks.
What about evaling the same string with '|' replaced by '+' ?
eval('$result = ' . str_replace('|', '+', preg_replace('/[^0-9|]/', '', $row["Pending"])) . ';');
echo "$result\n";
Note preg_replace() to sanitize input. Can be avoided if input is already sanitized
I have several variables, for example:
$q1
$q2
$q3
...
$q50
I would like to combine them info one variable who puts them info the VALUE of an INSERT INTO. Maybe a for loop?
INSERT INTO $table_name ($column_names) VALUES($q1, $q2, $q3)
So it might look like this
INSERT INTO $table_name ($column_names) VALUES($combined_variables)
This way, i could just manually add another variable to the $q51 and it would populate VALUE automaticly.
Is this possible? Maybe somehing like this (but this does not work)
$combined_variables = '';
for( $i = 1; $i <= 50 $i++ ) {
$combined_variables .= 'q' . $i . ', ';
}
$combined_variables = substr($combined_variables, 0, -2); //minus 2 to remove the last space and comma
This should work for you:
(Here I start with $q1 and assign it to the array until the next $qX is not set)
<?php
$combined_variables = [];
$count = 1;
while(isset(${"q" . $count})){
$combined_variables[] = ${"q" . $count};
$count++;
}
?>
So as an example:
$q1 = 5;
$q2 = 2;
You would end up with following array:
Array ( [0] => 5 [1] => 2 )
And then you can simply use it in the query like this:
"INSERT INTO $table_name ($column_names) VALUES(" . "'" . implode("','", $combined_variables) . "'" . ")"
You can use variable variables like this:
$combined_variables = array();
for ($i = 1; $i <= 50 $i++) {
$var = 'q' . $i;
$combined_variables[] = ${$var};
}
$combined_variables = implode(', ', $combined_variables);
However, if you can use an array instead of 50 variables you'd have a much easier job.
i want to get only 4 values from while loop in different variables.For example i want like this
$Var1 = "value1";
$Var2 = "value2";
$Var3 = "value3";
$Var4 = "value4";
here is my while loop
while($rw = $oAppl->row($rsimg))
{
//get value here
$var1 = $rw['thumb'];
}
But no value can be repeatable,i only have thumb returned.
If you want to set var1, 2, 3, 4 then stop, you need to keep track how many iterations the loop has gone through. (Alternatively a for loop may perform this task better)
$i = 1;
while ($rw = $oAppl->row($rsimg)) {
if ($i > 4) break;
eval("$Var" . $i . " = '" . $rw['thumb'] . "';");
$i++;
}
If storing values in $varNUM is not a requirement, I would suggest using an array to store these variables instead of using separate variables.
you cannot store more than one value inside a variable, you can either echo while looping
while($rw = $oAppl->row($rsimg))
{
//get value here
echo $rw['thumb'] . '<br>';
}
or create a new array to use all values later
$thumbs = array();
while($rw = $oAppl->row($rsimg))
{
//get value here
$thumbs[] = $rw['thumb'];
}