This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Appending a value of a variable to a variable name?
I can't figure out the syntax at all and have searched far and wide.
I would like to do this:
$uni = "ntu";
$selectedntu = "something";
echo $selected$uni;
// output should be the same as
echo $selectedntu;
In other words I'd like to use the contents of the second variable $uni to join onto the first variable's name. $selectedntu has been set with a foreach loop, but I can't figure out how to reference the two variables together in php.
Construct the string and use a variable variable $$
$uni = "ntu";
$selected = "something";
$new_variable = $selected . $uni;
echo $$new_variable;
// Or..per your googleing..purely for reference for others later
$uni = "ntu";
$selected = "something";
echo ${$selected . $uni};
Related
This question already has answers here:
MySQL Results as comma separated list
(4 answers)
Comma separated string of selected values in MySQL
(10 answers)
Just need a comma separated list from PHP/MySQL query [duplicate]
(5 answers)
Closed 2 years ago.
My goal: insert into a variable an array of items coming from a query and then print it
steps:
launch the query
do something with php
print the variable $report = 'this is your list: $list';
tried this:
$myquery = mysqli_query($dbconnection, "SELECT ...");
while($row = mysqli_fetch_assoc($myquery)) {
$my_array = $row['field'];
echo "$my_array, "; /* here I've the full list*/
}
$report = 'this is your list:' .$my_array. '.';
echo "$report"; /*I've only one item and not all the list*/
Your first echo is called several times because it is in the loop.
In every iteration you are replacing your content of $my_array.
Instead, try to attach it:
$my_array[] = $row['field'];
For more information see https://www.php.net/manual/de/language.types.array.php
This is normal.
Your while loop doing a "step" for each row found with your query.
You assigned $my_array as the "column" with the name field.
So at the end of the while loop, you'll get only the last column field of the last row.
Instead, try this
$myquery = mysqli_query($dbconnection, "SELECT ...");
$myWholeList = ''; // HERE
while($row = mysqli_fetch_assoc($myquery)) {
$my_array = $row['field'];
echo "$my_array, ";
$myWholeList .= '$my_array '; // HERE
}
$report = 'this is your list:' .$my_array. '.';
echo "$myWholeList"; // HERE
I've done a string concatenation, but you can do it with array and print_r() function. Have a look on this thread to see how to append data to an array.
mysqli_fetch_array while loop columns
EDIT:
Based on #isabella 's comment, she wants to display 7 items of array.
Two way :
Use print_r() or var_dump() which is the best to display an array (without taking care about rendering)
Add to $myWholeList variable each item.
e.g.
$myquery = mysqli_query($dbconnection, "SELECT ...");
$myWholeList = ''; // HERE
while($row = mysqli_fetch_assoc($myquery)) {
$my_array = $row['field'];
echo "$my_array, ";
// HERE
foreach($row as $field) {
$myWholeList .= $field . ' ';
}
$myWholeList .= '<br>'; // NEW LINE
}
$report = 'this is your list:' .$my_array. '.';
echo "$myWholeList"; // HERE
This question already has answers here:
How to check if a string is one of the known values?
(4 answers)
Closed 3 years ago.
I'm trying to echo entries from a database that have a lend_status of 1 or 2 and have written my while loop to get the entries but I'm not sure if my IF statement is correct or if this is the best way to go about this. I'm very new to PHP and SQL so any help would be really appreciated!
I've tried using an echo to see where it breaks and it seems to be the IF statement. I know the connection to the database works as I can echo information from it and have tested that.
<?php
// WHILE LOOP TO LOOK THROUGH THE DIFFERENT ROWS
$STH = $DBH->prepare("SELECT * FROM laptop_system");
// DOESN'T HAVE TO BE AN ARRAY BUT GOOD PRACTICE TO PUT THIS IN (BELOW)
$STH->execute(array());
// WHILE LOOP TO LOOK THROUGH THE DIFFERENT ROWS
while ($row = $STH->fetch()) {
$lend_status = $row->lend_status;
$lend_id = $row->lend_id;
$requestee = $row->user_id;
$first_name = $row->first_name;
$last_name = $row->last_name;
$active_status = array(1,2);
if($lend_status == $active_status) {
echo 'hello';
?>
<div>
<?php echo $requestee_name . '\n'; ?>
<?php echo 'hi'; ?>
</div>
<?php }} ?>
The problem is that you are comparing a string / integer - one of the fields of your database row - to an array:
$lend_status = $row->lend_status;
...
$active_status = array(1,2);
if($lend_status == $active_status) {
If you want to check if the status is one of the values in the array, you can use for example in_array():
if (in_array($lend_status, $active_status)) {
This question already has answers here:
Create new variables from array keys in PHP
(5 answers)
Closed 4 years ago.
Is there a way to pass all array's elements to variables with the elements' names?
For example if I have the following array:
$test['name']
$test['phone']
$test['address']
etc..
I want a shortcut for typing this:
$name = $test['name'];
$phone = $test['phone'];
$address = $test['address'];
etc..
Sure you can use $$
$test['name'];
$test['phone'];
$test['address'];
$test['name'] = "John";
$test['phone'] = "987987987";
$test['address'] = "Asheville";
foreach($test as $key=>$val){
$$key = $test[$key];
}
echo $phone;
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
No I'm doing wrong, I am modifying a system created by someone else but I can not show the information ... This is the code ...
$db = DB::getInstance();
$id = 1;
$query = $db->query("SELECT * FROM users WHERE id = ?", array($id));
$x = $query->results();
echo $x;
The error: Notice: Array to string conversion in...
Try using var_dump (instead of echo) if the return value is an object. For example:
var_dump($x);
use print_r($x) instead of echo $x
echo is used to print string and numbers but could not print array,
you may use var_dump too ... actually var_dump is used to print objects
use FOR :
$db = DB::getInstance();
$id = 1;
$query = $db->query("SELECT * FROM users WHERE id = ?", array($id));
$x = $query->results();
for ($i=0;$i<count($x); $i++){
echo $x[$i]."<br/>";
}
This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 1 year ago.
I am working on a recipe site for a school project, and I am trying to echo out two columns of arrays from my database in mysqli.
The arrays look like this:
And when I echo them out I would like them too look like this:
I have literally tried everywhere to find and answer.
My database name is "opskriftreg", and the connection to it works, the rest of the code comes out.
You can use explode to make an array of it,
Example:
$data = "hi,hello,helooo";
$my_array = explode($data);
foreach($my_array AS $value)
{
echo $value."<br />";
}
You are trying to complicate things?! simply get the two columns using a normal MySQL query, and do the job through your language:
select column1, column2 from table;
get the result in a variable of your used language, $array_of_values for example.
for every field in the array explode the value by the delimiter ','.
draw your new table using a loop that goes through the new arrays and write the values in HTML.
For example in PHP:
$array_of_values = array('ing1, ing2, ing3', 'amount1, amount2, amount3');
$ings = explode(',',$array_of_values[0]);
$amounts = explode(',',$array_of_values[1]);
echo "<table>";
for ($i=0; $i < count($ings); $i++){
echo "<tr>";
echo "<td>".$ings[$i]."</td>";
echo "<td>".$amounts[$i]."</td>";
echo "<tr/>";
}
echo "</table>";