Reputation: 35
I´m trying using php to generate a set of "{URL.callback}" to use in javascript. By using database. Here is my code:
$sql="select * from search where keywords LIKE '$bb%' ORDER BY keywords ";
$result=mysql_db_query($dbname,$sql);
echo $_GET["callback"] . "({ t:\"$t\", r:[";
while ( $rs=mysql_fetch_array($result)) {
$keywords=$rs[keywords];
echo "" ."\"$keywords\"".","." ";
}
echo"] })";
This is the code it returns:
({ t:"", r:["example1", "example2", "example3",] })
Everything seemed to be correct except the (,) in the end (after "example3") that I want to get rid. because it´s not correct and can not be use with that.
The question is: How to generate it correctly ? How to get rid the last (,) ?
Upvotes: 2
Views: 134
Reputation: 58601
I think the cleanest way, is to create an array of elements, then implode them with a ,
as separator.
// added initial `"`
echo $_GET["callback"] . "({ t:\"$t\", r:[\"";
while ( $rs=mysql_fetch_array($result)) {
$keywords=$rs[keywords];
$arr[] = $keywords;
}
echo implode('", "', $arr);
// added closing `"`
echo "\"] })";
Upvotes: 0
Reputation: 63852
Instead of generating the JSON yourself use json_encode
to do the heavy lifting for you.
$keywords = array ();
while ( $rs=mysql_fetch_array($result)) {
$keywords[] = $rs['keywords'];
}
echo json_encode ($keywords);
Preferred solution
Though if you plan on using json_encode
append $keywords to your other set of data and use json_encode
on the whole structure.
$json_data = array (
't' => '',
'r' => array ()
);
while ( $rs=mysql_fetch_array($result)) {
$json_data['r'][] = $rs['keywords'];
}
echo "(".json_encode ($json_data).")";
If you'd like to do it yourself there are a few (rather clean) options available, see the snippets below.
Append all retrieved keywords to an array and use join
with ',' as delimiter to form a string after your while loop
$keyword_array = array ();
while ($rs=mysql_fetch_array($result)) {
$keywords=$rs[keywords];
$keyword_array = "\"$keywords\"";
}
echo join (', ', $keywords);
Use mysql_num_rows
before your while loop to get the number of rows in $result
, and don't append a ,
when you are processing the last element.
$n_rows = mysql_num_rows ($result);
for ($i =1; $rs = mysql_fetch_array ($result); ++$i) {
$keywords=$rs[keywords];
echo "\"$keywords\"";
if ($i != $n_rows)
echo ", ";
}
Upvotes: 3
Reputation: 2471
Try this:
$output = '';
while ( $rs=mysql_fetch_array($result)) {
$keywords=$rs[keywords];
$output .= "" ."\"$keywords\"".","." ";
}
$output = substr($output, 0, -2);
echo $output;
Upvotes: 1
Reputation: 137420
Create data structure (multi-dimensional array) within PHP and let json_encode()
generate JS data structure.
It could look like this:
$sql="select * from search where keywords LIKE '$bb%' ORDER BY keywords ";
$result=mysql_db_query($dbname,$sql);
$data = array(
't' => $t,
'r' => array(),
);
while ( $rs=mysql_fetch_array($result)) {
$data['r'][] = $rs['keywords'];
};
and then display it like that:
echo $_GET["callback"].'('.json_encode($data).');';
Upvotes: 0
Reputation: 206
Hope this works for you
$sql="select * from search where keywords LIKE '$bb%' ORDER BY keywords ";
$result=mysql_db_query($dbname,$sql);
echo $_GET["callback"] . "({ t:\"$t\", r:[";
$keywords_print = array();
while ( $rs=mysql_fetch_array($result)) {
$keywords=$rs[keywords];
$keywords_print[] = "\"$keywords\"";
}
echo implode(",",$keywords_print);
echo"] })";
Upvotes: 0