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'];
}
Related
I'm currently have a recordset created with dreamweaver and have encode the results in json format which work fine.
Recordset
$maxRows_rs_feeds = 3;
$pageNum_rs_feeds = 0;
if (isset($_GET['pageNum_rs_feeds'])) {
$pageNum_rs_feeds = $_GET['pageNum_rs_feeds'];
}
$startRow_rs_feeds = $pageNum_rs_feeds * $maxRows_rs_feeds;
mysql_select_db($database_vivalooks, $vivalooks);
$query_rs_feeds = "SELECT fname,lname FROM profile";
$query_limit_rs_feeds = sprintf("%s LIMIT %d, %d", $query_rs_feeds, $startRow_rs_feeds, $maxRows_rs_feeds);
$rs_feeds = mysql_query($query_limit_rs_feeds, $vivalooks) or die(mysql_error());
$row_rs_feeds= mysql_fetch_assoc($rs_feeds);
if (isset($_GET['totalRows_rs_feeds'])) {
$totalRows_rs_feeds = $_GET['totalRows_rs_feeds'];
} else {
$all_rs_feeds = mysql_query($query_rs_feeds);
$totalRows_rs_feeds = mysql_num_rows($all_rs_feeds);
}
$totalPages_rs_feeds = ceil($totalRows_rs_feeds/$maxRows_rs_feeds)-1;
do {
echo json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
mysql_free_result($rs_feeds);
Recordset Results
{"fname":"Benjamin","lname":"Blay"}{"fname":"Alfread","lname":"Mark"}{"fname":"yaa","lname":"tiwaa"}
But i want the results to be encode like this rather
[{"fname":"Benjamin","lname":"Blay"},{"fname":"Alfred","lname":"Mark"},{"fname":"yaa","lname":"tiwaa"}]
Try
do {
$json[]=json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo "[".implode(", ", $json)."]";
OR
do {
$rows[] = $row_rs_feeds;
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo json_encode($rows);
You can change your loop to look like this:
echo "[";
$count = 0;
do {
if($count !== 0)
echo ",";
$count++;
echo json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo "]";
$count is there just to skip first "," so you don't get [,{...},{...}] or you can put it like false/true... I just can't remember right now what is better in case you have 10k steps loop...
Try:
do {
$rows[] = $row_rs_feeds;
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo json_encode($rows);
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'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 */
}
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;
I have a little problem I can't figure out how to solve.
I have an SQL result with 3 rows and I want to put the id from each row into a static marker ie.
MARKER_1 = 4
MARKER_2 = 5
MARKER_3 = 6
How can I do that so I get my static markers but with dynamic values?
I can't do it with a normal
while($row = mysql_fetch_array($result)) {
}
$i = 1;
while($row = mysql_fetch_array($result)) {
if($i == 1) {
$marker_1 = $row;
} elseif($i == 2) {
$marker_2 = $row;
} elseif($i == 3) {
$marker_3 = $row;
}
$i++;
}
i would recommend using an array like this
$results = array();
while($row = mysql_fetch_array($result)) {
$results[] = $row;
}
and then access it via:
$results[0] // or $results[1] and so on. you can even loop that :)
hope that helps
Hope this helps
while($row = mysql_fetch_array($result)) {
echo "MARKER_".$row['id'];
echo"=". $row['value'];
}