I was trying something but I need to explode it twice because I store 2 variables in a string. Anyways, I used a while loop but I don't understand, I use cid++, but it does not appear to increase. ANyways, here's the code.
$cid = 0;
while($row = mysql_fetch_array($result)){
$comment = explode("-", $row['comments']);
$madeby = explode("///", $comment[$cid]);
$cid++;
echo $madeby[1];
}
Since you're setting a var and incrementing you can use it as a key for both arrays.
$cid = 0;
while($row = mysql_fetch_array($result)){
$comment[$cid] = explode("-", $row['comments']);
$madeby[$cid] = explode("///", $comment[$cid]);
echo $madeby[$cid][1];
$cid++;
}
Then you can do this:
foreach($madeby as $key=>$tempArr){
echo '"'.$madeby[$key][0].'" by "'.$madeby[$key][1].'"<br>';
}
To see the whole array:
print_r($madeby);
try this
while($row = mysql_fetch_array($result))
{
$comment = explode("-", $row['comments']);
foreach($comment as $each_comment)
{
$madeby = explode("///", $each_comment);
echo $madeby[1];
}
}
or if you really want to use cid then
while($row = mysql_fetch_array($result))
{
$comment = explode("-", $row['comments']);
for($cid=0;$cid<count($comment);$cid++)
{
$madeby = explode("///", $comment[$cid]);
echo $madeby[1];
}
}
try this:
while($row = mysql_fetch_array($result)){
$comment = explode("-", $row['comments']);
$cnt = count($comment);
$madeby = array();
for($cid=0; $cid<$cnt; $cid++){
$madeby[] = explode("///", $comment[$cid]);
}
print_r($madeby);
}
Related
I'm want string out of the column data.
But it failed.
<?php
$conn = mysql_connect("localhost", "nantawat", "12345678") or die(mysql_error());
$select_db = mysql_select_db("my_db", $conn) or die(mysql_error());
$select_tbl = mysql_query("SELECT * FROM log", $conn);
while ($fetch = mysql_fetch_object($select_tbl)) {
$r = $fetch->data;
$i = explode(",", $r);
if (!isset($i[1])) {
for ($j = 0; $j <= 200; $j++) {
$i[$j] = null;
}
}
$name = $i[0];
$mama = $i[1];
$no = $i[2];
$a = $i[3];
$b = $i[4];
echo $name . "</br>";
echo $mama . $no . $a . $b . "</br>";
}
while ($data = mysql_fetch_object($select_tbl)) {
echo $data->data . "<br>";
}
?>
But i want output =
bus
car
bike
aabus
car
bike
aabus
car
bike
aabus
ddd
ee
And i not
Notice: Undefined offset: 3 in C:\xampp\htdocs\logs\New folder
(2)\explode.php on line 21
Notice: Undefined offset: 4 in C:\xampp\htdocs\logs\New folder
(2)\explode.php on line 22
Thank You.
You should just do what you want to do.
You want to connect to database then do it:
$conn = mysql_connect("localhost", "nantawat", "12345678") or die(mysql_error());
I suggest you to use mysqli library instead of mysql (mysql is deprecated in new php versions and totally removed in php7)
$conn = mysqli_connect("localhost", "nantawat", "12345678", "my_db") or die(mysql_error());
You want to query on log table, then do it:
$select_tbl = mysqli_query($conn, "SELECT * FROM log");
You want to fetch info from your result, then do it:
while ($row = mysqli_fetch_array($select_tbl)) {
echo $row['id_user'];
echo $row['id_doc'];
echo $row['date'];
echo $row['data'];
}
You want to explode data, then do it:
while ($row = mysqli_fetch_array($select_tbl)) {
echo $row['id_user'];
echo $row['id_doc'];
echo $row['date'];
$data = explode(',', $row['data']);
foreach ($data as $d) {
if ($d !== '') { // because before first comma or after last can be empty
echo $d . PHP_EOL;
}
}
}
If you want to save database result in variables:
If you are getting only one row of database, you can save them in variables directly:
$id_user = '';
$id_doc = '';
$date = '';
$data = array();
id ($row = mysqli_fetch_array($select_tbl)) {
$id_user = $row['id_user'];
$id_doc = $row['id_doc'];
$date = $row['date'];
$tempData = explode(',', $row['data']);
foreach ($tempData as $d) {
if ($d !== '') {
$data[] = $d;
}
}
}
And if you have multiple rows of database you need to save them all in a total array:
$array = array();
id ($row = mysqli_fetch_array($select_tbl)) {
$id_user = $row['id_user'];
$id_doc = $row['id_doc'];
$date = $row['date'];
$data = array();
$tempData = explode(',', $row['data']);
foreach ($tempData as $d) {
if ($d !== '') {
$data[] = $d;
}
}
$array[] = array(
'id_user' => $id_user,
'id_doc' => $id_doc,
'date' => $date,
'data' => data,
);
}
And finally use this to see what structure your final array has:
echo '<pre>';
pront_r($array);
echo '</pre>';
First off it is not wise to store comma seperated values in a single cell and you are using deprecated mysql_ functions. I think your solution can be found in using a foreach instead of the isset part:
while ($fetch = mysql_fetch_object($select_tbl)) {
$r = $fetch->data;
$i = explode(",", $r);
foreach ($i as $q){
echo $q . '<br/>';
}
}
If you still want to access your variables $name, $mama, $no and $ab, you can use isset for those specifically.
while ($fetch = mysql_fetch_object($select_tbl)) {
$r = $fetch->data;
$i = explode(",", $r);
if (isset($i[0])){
$name = $i[0];
echo $name . '<br>'; //only echo if it exists
}
if (isset($i[1])){
$mama= $i[1];
echo $mama. '<br>'; //only echo if it exists
}
//try it yourself for $no and $ab
}
Try:
while ($row = mysqli_fetch_array($select_tbl)) {
extract($row);
/* Using extract method can get the array key value as variable
Below variables are available
$id_user;
$id_doc;
$date;
$data; */
}
I am trying to create a drop down menu of usernames or a <select> as it's known in HTML. However I am only getting the last value back from my array and I can't figure out why.
PHP
function getUserName($db) {
try {
$sql = 'SELECT members.name FROM members';
$query_an = $db->query($sql);
$count = $query_an->rowCount();
if ($count > 0) {
while ($row = $query_an->fetch(PDO::FETCH_ASSOC)) {
$names = array();
$names[] = $row['name'];
}
return $names;
}
} catch(PDOException $e) {
die($e->getMessage());
}
}
HTML
<select>
<?php $names = getUserName($db); foreach($names as $key => $value) { ?>
<option value="<?php echo $key ?>"><?php echo $value ?></option>
<?php }?>
</select>
I'm fairly sure the HTML section of my code is solid. I think the error lies in how I'm adding values to my $names array but after staring at it for a half an hour I can't see it. Thanks for any help/fresh eyes.
You need to declare your array outside the loop
if ($count > 0) {
$names = array();
while ($row = $query_an->fetch(PDO::FETCH_ASSOC)) {
$names[] = $row['name'];
}
return $names;
}
this is emptying your array every time : $names = array();
use this :
while ($row = $query_an->fetch(PDO::FETCH_ASSOC)) {
$names[] = $row['name'];
}
You are creating a new $names array every row then returning the last one. You need to declare the array outside the while loop. The following should work:
function getUserName($db) {
try {
$sql = 'SELECT members.name FROM members';
$query_an = $db->query($sql);
$count = $query_an->rowCount();
if ($count > 0) {
$names = array();
while ($row = $query_an->fetch(PDO::FETCH_ASSOC)) {
$names[] = $row['name'];
}
return $names;
}
} catch(PDOException $e) {
die($e->getMessage());
}
}
The problem is you're re-initializing the array on every loop:
See:
while ($row = $query_an->fetch(PDO::FETCH_ASSOC)) {
$names = array();
$names[] = $row['name'];
}
return $names;
Should be:
$names = array();
while ($row = $query_an->fetch(PDO::FETCH_ASSOC)) {
$names[] = $row['name'];
}
return $names;
There is some problem, the code below assigning the last pr_name to all keys.
$arr = array();
while($row = mysql_fetch_array($results)) {
$keys[] = $row['pr_code'];
$items = array_fill_keys($keys, $row['pr_name']);
}
Simply with this:
$items = array();
while($row = mysql_fetch_array($results)) {
$items[$row['pr_code']] = $row['pr_name'];
}
function tableOne() {
$query = mysql_query("SELECT valor FROM grafico") or die(mysql_error());
$i = 0;
while($row = mysql_fetch_assoc($query)) {
$arr[] = array($row[valor]);
++$i;
}
echo json_encode($arr);
}
}
the output will be
[["15573"],["31978"],["11227"],["5752"],["20817"],["32182"]]
i need something like:
["15573","31978","11227","5752","20817","32182","10935"]
i tried some changes in the code but the output is not what i want.
thanks
You are placing sub-arrays in each element of your array. You should replace
$arr[] = array($row[valor]);
with
$arr[] = $row[valor];
The [] in $arr[] already adds each entry as an element of the array.
$query = mysql_query("SELECT valor FROM grafico") or die(mysql_error());
$arr = array();
while ($row = mysql_fetch_assoc($query)) {
$arr[] = $row['valor']; // get rid of the array() wrapper
}
echo json_encode($arr);
Here is the code
$i=0;
while ($row = mysql_fetch_array($result))
{
$output.="idno".$i."=".urlencode($row['usr_id']).
'&'."res".$i."=".urlencode($row['responce']).
'&'."sex".$i."=".urlencode($row['sex']).
'&'."com1".$i."=".urlencode($row['com1']).
'&'."com2".$i."=".urlencode($row['com2']);
$i++;
}
OUTPUT i get idno is part of com2 string how do I seperate them.
You need to add an & when $i is not zero:
$i=0;
while ($row = mysql_fetch_array($result))
{
$output .= ($i ? '&' : '') . "idno".$i."=".urlencode($row['usr_id']).
'&'."res".$i."=".urlencode($row['responce']).
'&'."sex".$i."=".urlencode($row['sex']).
'&'."com1".$i."=".urlencode($row['com1']).
'&'."com2".$i."=".urlencode($row['com2']);
$i++;
}
Another solution would be using an array and join its elements afterwards:
$array = array();
$i = 0;
while ($row = mysql_fetch_array($result)) {
$array[] = "idno$i=".urlencode($row['usr_id']);
$array[] = "res$i=".urlencode($row['responce']);
$array[] = "sex$i=".urlencode($row['sex']);
$array[] = "com1$i=".urlencode($row['com1']);
$array[] = "com2$i=".urlencode($row['com2']);
$i++;
}
$output .= implode('&', $array);
Furthermore you could use the argName[] declaration that PHP will convert into an array when receiving such a request query string.
Or you could do this:
$array = array();
$i = 0;
while ($row = mysql_fetch_array($result)) {
$array["idno$i"] = $row['usr_id'];
$array["res$i"] = $row['responce'];
//etc.
$i++;
}
$output = http_build_query($array);