It won't loop in a foreach or while - php

I'm trying to make this take 6 times, and then roll over into another row. I'm sure there's a better way of going about this, and that's probably why mine doesn't work.
However, when I use the foreach() it displays nothing, and when I use the while() the page breaks completely. The headers don't send, and php_error doesn't catch it.
All the queries work just fine, it's the display that's causing issues.
Perhaps you guys can help?
public static function DisplayInv($user) {
$user = users::lookup($user);
$sql = mysql_query("SELECT * from `inventory` where `userid`='{$user['id']}'");
$limit = 6;
$count = 0;
$fetch = mysql_fetch_array($sql) or die('Error: '.mysql_error());
while($count <= 7) {
print "<div class='row-fluid show-grid'>";
foreach($fetch as $items) {
$getItem = self::ItemInfo($items['itemid']);
print "<span class='span2'><b>{$getItem['name']}</b><br />{$getItem['description']}<br /><b>Power: {$getItem['power']}</b></span>";
$count++;
}
/* while($items = mysql_fetch_array($sql)) {
$getItem = self::ItemInfo($items['itemid']);
print "<span class='span2'><b>{$getItem['name']}</b><br />{$getItem['description']}<br /><b>Power: {$getItem['power']}</b></span>";
$count++;
}*/
print "</div>";
if($count == 6) {
$count = 0;
}
}
}

I guess you are looking for something like this?
public static function DisplayInv($user) {
$user = users::lookup($user);
$sql = mysql_query("SELECT * from `inventory` where `userid`='{$user['id']}'");
$limit = 6;
$count = 0;
print "<div class='row-fluid show-grid'>";
while($items = mysql_fetch_array($sql)) {
$getItem = self::ItemInfo($items['itemid']);
print "<span class='span2'><b>{$getItem['name']}</b><br />{$getItem['description']}<br /><b>Power: {$getItem['power']}</b></span>";
if($count == $limit){
print "</div><div class='row-fluid show-grid'>";
$count = 0;
}else{
$count++;
}
}
print "</div>";
}

Related

Looping problems in php

So I'm making this system where you can reserve a seat but when I'm doing a check if the seat is already reserved it screws up(Means that I'm too bad with loops). I have 3 test users in my database and it echoes the seats as many times as there are users. And it even gets the reserved seats correctly coloured.
$sql = "SELECT * FROM users";
$result = mysql_query($sql);
$list = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$reserved = $row["seat"];
$list[] = $reserved;
}
echo '<div id="top-section">';
for($i = 0; $i < 44; $i++) {
$seatCount++;
foreach($list as $obj) {
if($seatCount == $obj) {
echo '<div id="seat_'.$seatCount.'" class="seat reserved">'.$seatCount.'</div>';
}
else {
echo '<div id="seat_'.$seatCount.'" class="seat available" onclick="selectSeat('.$seatCount.');">'.$seatCount.'</div>';
}
}
}
echo '</div>';
And the result is this: http://i.gyazo.com/d10e787cea7028b46c716ac41766456a.png (I have three divs of seats and only done the loop on the top part so don't mind the 45 - 68 since they are correct). How do I make it so it only posts the seats once?
You don't need to have that inner foreach statement, if I think I know what you're going for.
There's two things wrong with your code, though.
Firstly, this statement is a little redundant
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$reserved = $row["seat"];
$list[] = $reserved;
}
Change it to this, you know, for simplicity:
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$list[] = $row["seat"];
}
Then, the loop can look a little something like this (which uses in_array instead of another loop):
for($i = 0; $i < 44; $i++) {
$seatCount++;
if(in_array($seatCount, $list)) {
echo '<div id="seat_'.$seatCount.'" class="seat reserved">'.$seatCount.'</div>';
} else {
echo '<div id="seat_'.$seatCount.'" class="seat available" onclick="selectSeat('.$seatCount.');">'.$seatCount.'</div>';
}
}
Then, this way, the $seatCount variable will be looked for in the user seats and if it is, it will show the reserved seat and if not will just show a selectable seat.
Your foreach ($list as $obj) is inside the loop. You have three reservations, so it's printing the seat three times. You need to move this outside the loop.
Use in_array():
for ($i = 0; $i < 44; $i++) {
if (in_array($i, $list)) {
echo '<div id="seat_'.$seatCount.'" class="seat reserved">'.$seatCount.'</div>';
}
else {
echo '<div id="seat_'.$seatCount.'" class="seat available" onclick="selectSeat('.$seatCount.');">'.$seatCount.'</div>';
}
}
Even better would be to make $list use the reserved seats as keys, not values:
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$reserved = $row["seat"];
$list[$reserved] = true;
}
Then your second loop could use:
if (isset($list[$i]))
You could make something like this:
$sql = "SELECT * FROM users";
$result = mysql_query($sql);
$list = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$reserved = $row["seat"];
$list[$reserved] = 1;
}
echo '<div id="top-section">';
for($i = 0; $i < 44; $i++) {
$seatCount++;
if(isset($list[$seatCount]) AND $list[$seatCount] == 1) {
echo '<div id="seat_'.$seatCount.'" class="seat reserved">'.$seatCount.'</div>';
}
else {
echo '<div id="seat_'.$seatCount.'" class="seat available" onclick="selectSeat('.$seatCount.');">'.$seatCount.'</div>';
}
}
echo '</div>';
You should use in_array() and remove one foreach loop.
<?php
$list = array(1,2,5);
echo '<div id="top-section">';
for($i = 0; $i < 44; $i++) {
if(in_array($i,$list)) {
echo '<div id="seat_'.($i+1).'" class="seat reserved">'.($i+1).'</div>';
}
else {
echo '<div id="seat_'.($i+1).'" class="seat available" onclick="selectSeat('.($i+1).');">'.($i+1).'</div>';
}
}
echo '</div>';
?>
Demo: https://eval.in/201702

for each output (sql) a different class

if ($result = $mysqli->query($query)) {
while ($list = $result->fetch_object()) {
$titel = $list->titel;
echo "<li class=\"wow fadeInLeft\">$titel </li> ";
}
$result->close();
}
I have several rows in a table. Some rows need the fadeInleft class, the second row needs fadeInRight class, the third row fadeInleft, the fourth row fadeInRight etc.
What's the best way?
Maybe there is a better way, but
$check=True;
while ($list = $result->fetch_object()) {
$titel = $list->titel;
$cls=$check ? "fadeInRight" : "fadeInLeft"
echo "<li class=\"wow $cls\">$titel </li> ";
$check=!$check
}
$result->close();
You want different classes for odd/even. Try this -
$i = 1;
while ($list = $result->fetch_object()) {
if($i %2 == 0) {
$class = 'fadeInRight';
} else {
$class = 'fadeInLeft';
}
//Use $class in li
echo "<li class='wow $class'>$titel</li>";
$i++;
}
You can use a modulo. Like this:
$i = 1;
if ($result = $mysqli->query($query)) {
while ($list = $result->fetch_object()) {
$titel = $list->titel;
$class = ($i % 2 == 0) ? 'fadeInLeft' : 'fadeInRight';
echo "<li class='wow $class'>$titel</li>";
}
$result->close();
}
Or using bitwise:
$i = 1;
if ($result = $mysqli->query($query)) {
while ($list = $result->fetch_object()) {
$titel = $list->titel;
$class = ($i & 1) ? 'fadeInLeft' /*odd*/ : 'fadeInRight' /*even*/;
echo "<li class='wow $class'>$titel</li>";
}
$result->close();
}
Slightly shorter version of the solution by #paubo147 which removes the need for the extra variable and does the assignment at the same time as checking for the value.
$check=True;
while ($list = $result->fetch_object())
{
$titel = $list->titel;
echo "<li class=\"wow ".(($check=!$check) ? "fadeInRight" : "fadeInLeft")."\">$titel </li> ";
}
$result->close();

split images from array

i am getting links from a database and want to display the images but break line every 5 images. i can display images but need help, I'm sure its an if statement but don't know how to write it. i need to display
image1,image2,image3,image4,image5 break
image6,image7,image8,image9,image10 break
and so on, i have a total of 100 images
<?php
include('connect.php');
$query = "SELECT * FROM `image`";
$result = mysql_query($query);
$pics = array();
while($row = mysql_fetch_array($result)){
$pics[] = "\"".$row['src']."\"";
}
foreach($pics as $show){
echo "<img src=".$show.">";
}
?>
You can use another variable to count the entries
$count=0;
foreach($pics as $show){
echo "<img src=".$show.">";
if( $count % 5 == 0 ) echo "<br>";
$count++;
}
$count = 1;
foreach($pics as $show){
if($count < 6)
{
echo '<img src="'.$show.'">';
$count++;
}
else
{
echo '<br><img src="'.$show.'">';
$count = 1;
}
}

How can I check the value of the Next Row while looping using PHP/MYSQL?

I got previousRow of record using this code
<?php
$previousRow = array();
while ($temp = mysql_fetch_row($res2))
{
echo "<br>currentRow:".$temp[1];
echo "previousRow:".$previousRow[1];
$previousRow = $temp;
}
?>
oupout
currentRow:1previousRow:
currentRow:5previousRow:1
currentRow:6previousRow:5
currentRow:7previousRow:6
currentRow:8previousRow:7
How can I check the value of the next row replaced by Previous Row ?
Any help would be grateful.
If I get you correctly, then something like this would help?
$previousRow = array();
$currentRow = mysql_fetch_row($res2);
while ($currentRow) {
$nextRow = mysql_fetch_row($res2);
echo "<br>currentRow:".$currentRow[1];
echo "previousRow:".$previousRow[1];
echo "nextRow:".$nextRow[1];
$previousRow = $currentRow;
$currentRow = $nextRow;
}
Please try code given below.
$res = array();
while ($result = mysql_fetch_row($r)) {
$res[] = $result;
}
echo "<pre>";
foreach($res AS $index=>$res1){
echo "Current".$res1[1];
echo " Next" . $res[$index+1][1];
echo " Prev" . $res[$index-1][1]; echo "<br>";
}
thanks
I'd collect all the rows first, then walk through them with a for:
<?php
$rows = array();
while ($temp = mysql_fetch_row($res2))
{
$rows[] = $temp;
}
$rowCount = count($rows);
for ($i = 0; $i < $rowCount; $i++) {
echo "<br>currentRow:".$rows[$i][1];
if ($i > 0) {
echo "previousRow:".$rows[$i - 1][1];
}
if ($i + 1 < $rowCount - 1) {
echo "nextRow:".$rows[$i + 1][1];
}
}
?>

php mysql_fetch_array every 25 items wrap a div

I want to mysql_fetch_array 100 items from mysql database. then every 25 items wrap a div.
also, I make a total items result check.
My code as below, but it will add <div> to every item, no as my require. So how to make it easier? Thanks.
if (mysql_num_rows($result) != 0) {
$total = mysql_num_rows($result);
$num = 1;
while ($row = mysql_fetch_array($result)) {
if ($num === 1) {
echo '<div>';
}
echo $row['title'] . '<br />';
if ($total < 26) {
echo '</div>';
}
else {
if ($num === 26) {
echo '<div>';
}
if ($total < 51) {
echo '</div>';
}
else {
if ($num === 51) {
echo '<div>';
}
if ($total < 76) {
echo '</div>';
}
else {
if ($num === 75) {
echo '<div>';
}
if ($total < 101) {
echo '</div>';
}
}
}
}
$num++;
}
}
You can use the mod operator.
See: http://php.net/manual/en/internals2.opcodes.mod.php
echo '<div>';
while($row = mysql_fetch_array($result)){
....
if (($num % 25) === 1) { echo '</div><div>' }
$num++;
}
echo '</div>';
Try This...
if(mysql_num_rows($result)!=0){
$total = mysql_num_rows($result);
$num=1;
while($row = mysql_fetch_array($result)){
if($num===1)
echo '<div>';
echo $row['title'].'<br />';
if($num==25){
echo '</div>';
$num=1;
}
$num++;
}
}
use the mod operator.
$num =1;
while($row = mysql_fetch_array($result)){
if (($num % 25) === 1) { echo '<div>' }
-------here data ----
if (($num % 25) === 0) { echo '</div>' }
$num++;
}
My advice is to separate your concerns... Give it a shot more like this:
// first get all your data.
$results = array();
while($row = mysql_fetch_array($result)){
$results[] = $row;
}
// now you have an array of all of your records.
if (!empty($results)) {
// total count of the array up front.
$total = count($results);
$interval = 0;
// save the data in a buffer for echoing later.
$buffer = array('<div>');
foreach($results as $row) {
$buffer[] = $row['title'] . '<br />';
if (++$interval === 24) { // notice the prefix ++
$interval = 0;
// close and re-open divs
$buffer[] = '</div><div>';
}
}
// one final close tag
$buffer[] = '</div>';
// now we zip it up and echo the content.
echo implode('', $buffer);
}
Here is how I do it...
$count = 0;
while($row = mysql_fetch_array($result)){
if ($count%$25 == 0)
{
echo "<div>";
}
//whatever your data goes here
count++
if ($count%$25 == 0)
{
echo "</div>";
}
}

Categories