I'm trying to fetch arrays from two queries. The result only gets the first index.
while(($row2 = mysql_fetch_array($query1)) && ($index = mysql_fetch_array($query2)))
{
$leaveID = $row2['leaveID'];
$personID = $index['personID'];
$Person_Type = $index['Person_Type'];
if ($Person_Type == 'Regular')
{
$Sick_Remaining_Days = 10 - $Sick_Total_Days;
}else{
$Sick_Remaining_Days = 5 - $Sick_Total_Days;
}
echo "<tr>";
echo ($leaveID == $personID) ? "<td>$Sick_Remaining_Days</td>" : "<td>--</td>";
echo "</tr>";
}
Assuming both queries are OK I offer two ways to loop through them depending on your needs. Hopefully one of them will help.
<?php
$arrayRowA = array();
$arrayRowB = array();
while($row = mysql_fetch_array($query1)){$arrayRowA[] = $row;}
while($row = mysql_fetch_array($query2)){$arrayRowB[] = $row;}
// Loop through two arrays in a square way (every combination of both arrays)
foreach($arrayRowA as $keyA => $objectA){
foreach($arrayRowB as $keyB => $objectB){
$leaveID = $objectA['leaveID'];
$personID = $objectB['personID'];
$Person_Type = $objectB['Person_Type'];
if($Person_Type == 'Regular')
{
$Sick_Remaining_Days = 10 - $Sick_Total_Days;
}else{
$Sick_Remaining_Days = 5 - $Sick_Total_Days;
}
echo "<tr>";
echo ($leaveID == $personID) ? "<td>$Sick_Remaining_Days</td>" : "<td>--</td>";
echo "</tr>";
}
}
// Loop through two arrays in a linear way (one to one)
foreach($arrayRowA as $keyA => $objectA){
if(isset($arrayRowB[$keyA])){
$objectB = $arrayRowB[$keyA];
$leaveID = $objectA['leaveID'];
$personID = $objectB['personID'];
$Person_Type = $objectB['Person_Type'];
if($Person_Type == 'Regular')
{
$Sick_Remaining_Days = 10 - $Sick_Total_Days;
}else{
$Sick_Remaining_Days = 5 - $Sick_Total_Days;
}
echo "<tr>";
echo ($leaveID == $personID) ? "<td>$Sick_Remaining_Days</td>" : "<td>--</td>";
echo "</tr>";
}
}
?>
$query = mysql_query("select p*, l* from person as p left join leave as l on p.personID = l.leaveID");
while(($row = mysql_fetch_array($query)))
{
$Person_Type = $row['Person_Type'];
if ($Person_Type == 'Regular')
{
$Sick_Remaining_Days = 10 - $Sick_Total_Days;
}else{
$Sick_Remaining_Days = 5 - $Sick_Total_Days;
}
echo "<tr>";
echo "<td>$Sick_Remaining_Days</td>";
echo "</tr>";
}
You should get array before while:
array f,s
while(first)
{
f = ...;
}
while(second)
{
s = ...;
}
And now use both array.
Or try use new http://php.net/manual/en/mysqli-result.fetch-array.php. Probably you losing connection to first query after use second query.
while($row = mysqli_fetch_assoc($result))
{
$row2 = mysqli_fetch_assoc($result2);
echo $row['id'];
echo $row2['id'];
/* your code */
} `
Use this... It is what do you search !
while($row2 = mysql_fetch_array($query1))
{
$index = mysql_fetch_array($query2);
echo $row['id'];
echo $index['id'];
/* add code */
}
Related
Just stating learning PHP, I would like to know is there any way I can declare more than 1 $value in a foreach loop? I am trying to echo out my 8 different type of arrays which i declared ($line0 - $line8). Apologies if my codes are abit messy. I'm still new to PHP.
PHP Code
<?php
$handle = #fopen('listings.txt', "r");
$row = 0;
$count = 0;
$line0 = [];
$line1 = [];
$line2 = [];
$line3 = [];
$line4 = [];
$line5 = [];
$line6 = [];
$line7 = [];
$line8 = [];
if ($handle) {
while (!feof($handle)) {
$store = fgets($handle, 4096);
if ($row == 9){
$row = 0;
$count++;
}
if ($row == 0)
{
$line0[] = strval($store);
}
else if($row == 1) {
$line1[] = strval($store);}
else if($row == 2) {
$line2[] = strval($store);}
else if($row == 3) {
$line3[] = strval($store);}
else if($row == 4) {
$line4[] = strval($store);}
$row++;
}
?>
<table>
<tr>
<?php
foreach($line2 as $value1)&&(line3 as $value2){
echo "<td><b>Product ID: $value1</b>"
echo "<td><b>Selection ID: $value2</b>
</td>";
echo '</tr>';
}
?>
</table>
listings.txt
Cedric
93482812
cedric#hotmail.com
Guitar
---------------------------------------------
Wendy
98238432
wendy#hotmail.com
Guitar
---------------------------------------------
No you can't do that, but there's something else you can use. All your arrays have the name keys, and you can get the key with foreach like this:
foreach ($line1 as $key => $value1) {
$value2 = $line2[$key];
echo "<tr>";
echo "<td><b>Product ID: $value1</b></td>";
echo "<td><b>Selection ID: $value2</b></td>";
echo '</tr>';
}
That is getting pretty close to what you want.
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 want to display the values of my array. but instead of displaying:
1509 1510 1511 it display ArrayArrayArray. What does it mean?
include("db_PSIS.php");
$sql_dc = "SELECT Child, Datecode
FROM traveller_merging15
WHERE Parent='" . $_REQUEST["txt_traveller_no"] . "'
ORDER BY Merge_ID ASC";
$res_dc = mysql_query($sql_dc);
$dcode1 = $row_dc['Datecode'];
$storeArray = array();
if (!$res_dc) {
echo "No data fetched!";
} else {
while ($row_dc = mysql_fetch_array($res_dc, MYSQL_ASSOC)) {
$storeArray[] = $dcode1;
echo "{$storeArray} <br>";
}
//$str_dc=implode(",",$storeArray);
//echo $str_dc;
}
You are assign the value of $row_dc['Datecode']; before fetch data from database. You need to do fetch data inside while loop and echo it
$res_dc = mysql_query($sql_dc);
if (!$res_dc) {
echo "No data fetched!";
} else {
while ($row_dc = mysql_fetch_array($res_dc, MYSQL_ASSOC)) {
echo $row_dc['Datecode'];
}
}
Note:- mysql is deprecated instead use mysqli or PDO
else {
while ($row_dc = mysql_fetch_array($res_dc, MYSQL_ASSOC)) {
$storeArray[] = $dcode1;
echo "{$storeArray} <br>";
}
instead try
else {
while ($row_dc = mysql_fetch_array($res_dc, MYSQL_ASSOC)) {
$storeArray[] = $dcode1;
echo "{$storeArray[0]} <br>";
}
Try this,
else {
while ($row_dc = mysql_fetch_array($res_dc, MYSQL_ASSOC)) {
$child = $row_dc['Child'];
$Datecode = $row_dc['Datecode'];
echo "$child <br> $Datecode";
}
if you are getting more that 1 row, use for() loop
else {
while ($row_dc = mysql_fetch_array($res_dc, MYSQL_ASSOC)) {
$count = count($row_dc);
for($i = 0; $i < $count; $i ++){
$child = $row_dc[$i]['Child'];
$Datecode = $row_dc[$i]['Datecode'];
echo "$child <br> $Datecode";
}
}
-first array don't print value using echo
-use print_r($array) to print key value pair(print a array)
your solution is
$i = 0;
while ($row_dc = mysql_fetch_array($res_dc, MYSQL_ASSOC)) {
$storeArray[$i] = $dcode1;
echo $storeArray[$i].'<br>';
$i++;
}
NOTE::-- Use single Quote instead of double Quote and mysql is deprecated instead use mysqli or PDO
I have this timetable where a first loop a bunch of members from the database with and while-loop. Inside the while-loop I have an foreach-loop. How ever, the foreach-loops makes the whole timetable very slow, and I need an different approach to this problem.
What it looks like now.
$sql = mysql_query("SELECT * FROM tblstaff ORDER BY strName ASC");
while($row = mysql_fetch_array($sql))
{
$user_id = $row['intId'];
$arr = array($one, $two, $three, $four, $five, $six, $seven);
foreach ($arr as $w) {
$week = $w;
$sql_offtime = mysql_query("SELECT tbloffwork.intId, tbloffwork.intWhoId, tbloffwork.strWeek
FROM tblstaff, tbloffwork
WHERE tbloffwork.intWhoId = '$user_id' AND tbloffwork.strWeek = '$week'");
$rwOff = mysql_fetch_array($sql_offtime);
$sql_work = mysql_query("SELECT DISTINCT tblstaff.intId as staffId, tblstaff.strName, tblworktable.intId, tblworktable.intWhoId, tblworktable.strElapsed, tblworktable.strNotes
FROM tblstaff, tblworktable
WHERE tblworktable.intWhoId = '$user_id' AND tblworktable.strElapsed = '$week'");
$rw_work = mysql_fetch_array($sql_work);
if($rwOff['strWeek'] == $week) {
$td_stat = "timetable-td-background-yellow";
} elseif($rw_work['strElapsed'] != "") {
$td_stat = "timetable-td-background-green";
} else {
$td_week = "timetable-td-background-red";
}
if($rw_work['strNotes'] != ""){
$notes = "<i class='fa fa-pencil-square-o mediumsmall'></i>";
} else { $notes = $null; }
echo "<td align='right' valign='top' id='timetable-td-small-square' class='". $td_week ."''>". $notes ."</td>";
}
}
Is there a better way to approach this instead of going with the foreach method?
foreach makes a copy of the set, you can use while instead
while (list($key, $value) = each($arr)) {
echo "Key: $key; Value: $value<br />\n";
}
http://php.net/manual/en/control-structures.foreach.php
This is my partial code:
while($row = $db->fetch_array($query))
{
echo $row['row_name'];
}
How can I make it so it will add a break tag after each result, but not the last result?
Put the output into an array, then join the array with implode:
$rows = array();
while($row = $db->fetch_array($query))
{
$rows[] = $row['row_name'];
}
echo implode('<br/>', $rows);
You could do this. No arrays or counters.
if($row = $db->fetch_array($query))
{
do {
echo $row['row_name']
} while($row = $db->fetch_array($query) && print("<br />"));
}
for ($idx = 0; $row = $db->fetch_array($query); $idx++)
{
if ($idx > 0) { echo "<br/>"; }
echo $row['row_name'];
}