So I'm using x-editable for bootstrap, which is awsome for me.
With that, to return a menu for selection one needs to return an array as such to make it work:
$arr = array(
array('value' => 'Male', 'text' => 'Male'),
array('value' => 'Female', 'text' => 'Female'),
);
It's fine if you have to write it, but now I need to make this array from database output.
So for instance if I run a "while($row" loop, how is the output going to be an array like that. This is what I'm trying, but oviously failing at cause this is not working:
$query = 'SELECT id,app_name FROM apps';
$result = mysql_query($query) or bomb($s,509,addslashes($query),addslashes(mysql_error()));
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)) {
$arr .= Array(
Array('value' => $row['id'], 'text' => $row['app_name']),
);
}
}
It's probably a silly question, so thanks in advanced.
Arrays can't be concatenated like strings.
Try something like this:
while($row = mysql_fetch_assoc($result)) {
$arr[]=Array('value' => $row['id'], 'text' => $row['app_name']);
}
Related
I've searched through the stack for an answer but it seems that I cannot find a solution for the following code:
$servers = array();
while($row = mysql_fetch_array( $result ))
{
$servers[] = $row['name'] => array('ip' => $row['ip'],'port' => $row['port'],'info' => $row['info'],'purpose' => $row['purpose']),
}
What I'm trying to achieve is fetch all results but I receive the following error:
syntax error, unexpected '=>' (T_DOUBLE_ARROW).
Making it like this gets only the last item:
while($row = mysql_fetch_array( $result )) {
$servers = array(
$row['name'] => array('ip' => $row['ip'],'port' => $row['port'],'info' => $row['info'],'purpose' => $row['purpose']),
);
}
That being said, $servers is in the while loop, thus making it index only the last item. I don't know what causes the unexpected => :/
It looks like you're trying to do this:
$servers = [];
while($row = mysql_fetch_array( $result )) {
$servers[$row['name']] = [
'ip' => $row['ip'],
'port' => $row['port'],
'info' => $row['info'],
'purpose' => $row['purpose']
];
}
That gives you a final $servers array, indexed by server name, with an array of details.
Side note: as Jay commented, you really shouldn't be using mysql functions any longer, really encourage you to check out mysqli or PDO functions instead.
I am trying to make a dynamic JSON array in PHP, however when I try to do so it returns "Array". Here is the code I am currently using:
<?php
require '../../scripts/connect.php';
$array = '';
if($result = $db->query("SELECT * FROM art") or die ($db->error)){
if($count = $result->num_rows) {
while($row = $result->fetch_object()){
$array .= array(
'title' => $row->title,
'image' => "http://www.thewebsite.com/img/2.jpg",
'rating' => 7.7,
'releaseYear' => 2003,
'genre' => array(
'0' => $row->category,
'1' => $row->subcategory
)
);
}
}
}
echo json_encode($array);
?>
Can anyone suggest how I might go about fixing this?
And if anyone has suggestions about creating a dynamic JSON array, some help would be much appreciated.
Change your declaration of $array to be an array:
$array = array();
Then in your while loop, when you add the new array to $array, push it like this:
$array[] = array('title'=>$row->title, etc...)
The problem is here: I'm trying to write a code which would make a dynamic PHP array. I have an empty array, to which need to add keys with their values, values being passed by a do-while loop from the MySQL server.
The following php code:
$result=mysqli_query($link,"SELECT products_id,voted,rating FROM table_products $sorting LIMIT 6");
if(mysqli_num_rows($result)>0) {
$a = array();
do {
$row = mysqli_fetch_array($result);
$obj=$row['products_id'];
$rating=$row['rating'];
$voted=$row['voted'];
array_push($a,array('obj'.$obj => array('rating' =>
$rating, 'voted' => $voted)));
} while ($row = mysqli_fetch_array($result)
);}
$json = json_encode($a);
echo $json;
...gives me this json string:
[{"obj1":{"rating":"25","voted":"5"}},{"obj3":{"rating":"36","voted":"10"}},{"obj5":{"rating":"6","voted":"4"}}]
...but I need this:
["obj1":{"rating":"25","voted":"5"},"obj3":{"rating":"36","voted":"10"},"obj5":{"rating":"6","voted":"4"}]
So how can I push this:
'obj'.$obj => array('rating' => $rating, 'voted' => $voted)
whithout putting it in a separate array, like I did in my code? I just don't know a right way to implement this.
Change
array_push($a,array('obj'.$obj => array('rating' => $rating, 'voted' => $voted)));
To
$a['obj'.$obj] = array('rating' => $rating, 'voted' => $voted);
Try this instead of array_push
$a = array_merge($a, array(
'obj'.$obj => array(
'rating' => $rating,
'voted' => $voted
)
));
I am trying to create a JSON array with the information selected from a database but I cannot give the array a name.
while($row = mysql_fetch_array($result))
{
$arr = array('isim' => $row['ename'], 'yer' => $row['eplace'], 'top' => $row['society'], 'tar' => $row['edate'], 'saat' => $row['ehour']);
echo json_encode($arr);
}
I want to see the result;
{"events":[{"isim":"eere","yer":"dddd","top":"asdfsdffgdfgdfg","tar":"2013-10-18","saat":"12:46"}{"isim":"fhjfr","yer":"yhjrhj","top":"ryjryjrj","tar":"2013-10-30","saat":"12:45"}{"isim":"sfsgsg","yer":"sfgssfg","top":"sgsfgsg","tar":"2013-10-31","saat":"12:45"}]}
But I cannot see the
{"events":[
in the beggining and
]}
at the end.
Thank you.
To generate valid JSON, you first need to add everything to a multi-dimensional array and only then when that is complete, encode it:
$arr = array();
while($row = mysql_fetch_array($result))
{
$arr[] = array('isim' => $row['ename'], 'yer' => $row['eplace'], 'top' => $row['society'], 'tar' => $row['edate'], 'saat' => $row['ehour']);
// or perhaps just: $arr[] = $row;
}
echo json_encode($arr);
Also note that the mysql_* functions are deprecated.
To put everything under an events key, you would need something like:
$arr['events'][] = array('isim' => $row['ename'], 'yer' => $row['eplace'], 'top' => $row['society'], 'tar' => $row['edate'], 'saat' => $row['ehour']);
I have been looking around for PHP tutorials and I found a very detailed one that have been useful to me.
But now, I have a question. The results showed by the API are stored into a $results array. This is the code (for instance):
$fetch = mysql_fetch_row($go);
$return = Array($fetch[0],$fetch[1]);
$results = Array(
'news' => Array (
'id' => $return[0],
'title' => $return[1]
));
My question is.. I want to display the last 10 news.. how do I do this? On normal PHP / mySQL it can be done as:
while($var = mysql_fetch_array($table)) {
echo "do something"
}
How can I make it so the $results can print multiple results?
I have tried like:
while($var = mysql_fetch_array($table)) {
$results = Array(
'news' => Array (
'id' => $return[0],
'title' => $return[1]
));
}
But this only shows me one result. If I change the $results .= Array(...) it gives error.
What can I do?
Thanks!
Edit
My function to read it doesn't read when I put it the suggested way:
function write(XMLWriter $xml, $data){
foreach($data as $key => $value){
if(is_array($value)){
$xml->startElement($key);
write($xml, $value);
$xml->endElement();
continue;
}
$xml->writeElement($key, $value);
}
}
write($xml, $results);
$results[] = Array(
'news' => Array (
'id' => $return[0],
'title' => $return[1]
));
That should do it.
If you are familiar with arrays, this is the equivalent of an array_push();
array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed. Has the same effect as:
<?php
$array[] = $var;
?>
Use [] to add elements to an array:
$results = array();
while($var = mysql_fetch_array($table)) {
$results[] = Array(
'news' => Array (
'id' => $var[0],
'title' => $var[1]
));
}
}
You can then loop through the $results array. It's not an optimal structure but you should get the hang of it with this.