I'm using in PHP
while($x = mysql_fetch_array($y))
I want to select and show 3 records from my MySQL DB, than make in HTML code and show next 3 records from database.
How to do this?
$idx = 0;
$htmlStr = '<div>';
while($x = mysql_fetch_array($y)) {
if($idx % 3 == 0 && $idx>0) {
//here your part when 3 rows areready
echo $htmlStr.'</div>';
$idx = 0;
$htmlStr = '<div>';
}
$idx++;
$htmlStr .= '<p>'.implode(',',$x).'</p>';
}
if($idx % 3 == 0 ) {
echo $htmlStr.'</div>';
}
Apart from using LIMIT 3 in sql query, you can add counter:
$c = 0;
$e = $c + 3;
while($x = mysql_fetch_array($y) && $c < $e){
...
$c++;
}
Of course you can put that into another loop that will increment the initial $c value by 3 and continue retrieving values from this query result.
Related
I am reading excel file in php. I want to show the total of one column in the last row. To do that I need to get the total sum of below array.
part of code to get the below array is,
Array ( [0] => 1 ) Array ( [0] => 2 )
$file = "$filename";
$sheet = (isset($_POST['sheet'])) ? $_POST['sheet'] : '';
$connection = new Spreadsheet_Excel_Reader();
$connection->read($file);
echo"<table>";
$x = 1;
while ($x <= $connection->sheets[$sheet]['numRows']) {
echo "\t<tr>\n";
$y=1;
while ($y <= $connection->sheets[$sheet]['numCols']) {
$cell = isset($connection->sheets[$sheet]['cells'][$x][$y]) ? $connection->sheets[$sheet]['cells'][$x][$y] : '';
echo "\t\t<td>$cell</td>\n";
$y++;
}
echo "\t</tr>\n";
$x++;
}
$x = 2;
while ($x <= $connection->sheets[$sheet]['numRows']) {
$ctnqty = isset($connection->sheets[$sheet]['cells'][$x][16]) ?
$connection->sheets[$sheet]['cells'][$x][16] : '';
$ctnqtyttl = explode(",",$ctnqty);
print_r ($ctnqtyttl);
$x++;
}
I'm suing below code to get the sum of above array, but its giving me the result of 2 only. Please help me here to get the sum 3.
$sum = 0;
foreach ($ctnqtyttl as $value) {
echo $sum = $sum + $value;
}
$ctnqtyttl is overwritten each time with a new value in your second while loop, so it forgets any previous values. The sum you calculate will only be based on the last value it has.
Skip the final foreach loop, and calculate the sum during the second while loop as follows:
$sum = 0; // <-------
$x = 2;
while ($x <= $connection->sheets[$sheet]['numRows']) {
$ctnqty = isset($connection->sheets[$sheet]['cells'][$x][16]) ?
$connection->sheets[$sheet]['cells'][$x][16] : '';
$ctnqtyttl = explode(",", $ctnqty);
print_r ($ctnqtyttl);
$sum += array_sum($ctnqtyttl); // <-------
$x++;
}
I am pulling rows from a MySql DB, and I want to enter a line break on the 2nd row.
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$status = $row ['status'];
echo "$status";
}
if ($i % 4 == 0) {
echo '';
}
Is there a way of knowing if it is the 2nd row? Im guessing its something to do with $i?
Sorry if it is a silly question!
You're right. It has something to do with the $i variable.
If you want to enter it the 2nd row only you can evaluate $i==2. Also you could add it every 2nd row by evaluating $i % 2 == 0
This results in the following code:
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$status = $row ['status'];
echo "$status";
if ($i == 2){ // Or replace the evaluation with $i % 2 == 0
echo '<br />'; //HTML linebreak
//echo '\n'; //This is for a newline character.
}
}
if ($i % 4 == 0) {
echo '';
}
This should do it:
for ($i = 1; $i <= mysql_num_rows($result); $i++) {
$row = mysql_fetch_array($result);
$status = $row['status'];
echo "$status";
if ($i == 2) {
echo "\n"; //line break
}
}
if ($i % 4 == 0) {
echo '';
}
If you want an HTML line break, use:
echo "<br/>";
Your indenting might lead you to think the $i mod 4 is inside of the for loop when in fact it is not.
No questions are silly :)
I have an array that can have any number of items inside it, and I need to grab the values from them at a certain pattern.
It's quite hard to explain my exact problem, but here is the kind of pattern I need to grab the values:
No
Yes
No
No
No
Yes
No
No
No
Yes
No
No
I have the following foreach() loop which is similar to what I need:
$count = 1;
foreach($_POST['input_7'] as $val) {
if ($count % 2 == 0) {
echo $val;
echo '<br>';
}
$count ++;
}
However, this will only pick up on the array items that are 'even', not in the kind of pattern that I need exactly.
Is it possible for me to amend my loop to match that what I need?
You can do this much simpler with a for loop where you set the start to 1 (the second value) and add 4 after each iteration:
for ($i = 1; $i < count($_POST['input_7']); $i += 4) {
echo $_POST['input_7'][$i] . '<br />';
}
Example:
<?php
$array = array(
'foo1', 'foo2', 'foo3', 'foo4', 'foo5',
'foo6', 'foo7', 'foo8', 'foo9', 'foo10',
'foo11', 'foo12', 'foo13', 'foo14', 'foo15'
);
for ($i = 1; $i < count($array); $i += 4) {
echo $array[$i] . '<br />';
}
?>
Output:
foo2foo6foo10foo14
DEMO
Try this:
$count = 3;
foreach($_POST['input_7'] as $val) {
if ($count % 4 == 0) {
echo $val;
echo '<br>';
}
$count ++;
}
I have a query always results to two rows. How can i flag the first row equal 1 and the second is 0?
here is the code
$rows = array(
array('number' => 1),
array('number' => 2)
);
$i=1;
foreach($rows as $r) {
if($i == 1) {
$i = 1;
} else {
$i = 0;
}
//Flag first row as 1
//Flag second row as 0;
}
Is this correct?
Try using Modulus %:
foreach($rows as $index => $r) {
$i = ($index % 2 == 0) ? 1 : 0;
echo "<br/>" . $i;
}
as you say query always return two rows then try this:
foreach($rows as $key=>$r) {
if($key=="0") {
$i = 1;
} else {
$i = 0;
}
//Flag first row as 1
//Flag second row as 0;
}
Morning, I have a script tha is calling entries from a database, their history actually, and then displays the entries in a table, right now in order to create the table im using a pretty big for loop to make comparisons and add the results in. However for some reason it is timing out after the 30 seconds(the loop doesnt make that many cycles) i have dtermined that the cause is an issue with the first inner loop due to the fact that the echo statement in there just repeats for the duration of the loop, it never leaves that. any ideas?
for($i = 1; $i <= $FirstCount; $i++)
{
$HistoryTable .= "<tr>";
if($i = 1)
{
for($j = 0; $j < $ThirdCount; $j++)
{
if($EntryTwo[0][$j+1] == $EntryOne[$j])
{
$HistoryTable .= "<td></td>";
}
else
{
$HistoryTable .= "<td>".$EntryTwo[0][$j+1]."</td>";
}
echo $EntryTwo[0][$j+1].' == '.$EntryOne[$j];
}
}
else
{
$first = 0;
$second = 1;
for($k = 1; $k <= $SecondCount; $k++)
{
if($EntryTwo[$first][$k] == $EntryTwo[$second][$k])
{
$HistoryTable .= "<td>".$EntryTwo[$second][$k]."</td>";
}
else
{
$HistoryTable .= "<td></td>";
}
$first++;
$second++;
}
unset($k);
unset($first);
unset($second);
}
$HistoryTable .= "</tr>";
}
variables:
$FirstCount = 4;
$SecondCount = 18
$ThirdCount = 17
if($i = 1) is setting $i to 1 every time, so it's an infinite loop.
What you want is if ($i == 1).