Why counter resets when I click on other link? - php

I have define a counter in while loop and it is initialized outside the loop.
I have echo the counter in loop itself. When I have 50 record on first page it displays 1 to 25. The problem is that on second page it start again from 1, not like it should work: to start it from 26 to 50.
Here is my code:
<?php
//show records
$query = mysqli_query($con,"SELECT table2.col2 AS a,table1.col2 AS b, table1.col1 AS c, table1.q_url AS d FROM {$statement} LIMIT {$startpoint} , {$limit}");
$Authorname='';
$count=1;
while ($row = mysqli_fetch_array($query)) {
$output='';
$Authorname =$row['a'];
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url=explode('/',$url);
?>
<div class="record round"><?php echo $count;$output .='<a href="http://localhost/quotes/'.$url[5].'/'.$row['d'].'.html">';
echo $output .=$row['b'].'</a>';?></div>
<?php
$count++;
}
?>

Edit
Try
$nbpage = 2;
$nbresultperpage=25;
$count = ($nbpage-1)*$nbresultperpage;
Where $nbpage is you're current number of page

Related

Create Divs for each group of values

I need to group all rows with matching roomcode. When I also group resort, it acts like it disables the grouping of roomcode and only gives me the results of the first row with a roomcode.
How can I group the resort column and still group matching roomcode columns? If I ungroup my resort code the query works correctly, but then I can't create separate divs for differing resorts.
The user is searching a date range, hence having multiple rows with the same roomcode, which fetches my points. This is why I am getting the sum of those points. My current way of grouping each resort for output HTML purposes is breaking that. I don't necessarily have to group the resorts, I just need each room at each resort created within its own DIV set. I have a basic example below.
Ideal HTML output
<div id="results">
<div id="resort resortcode">
<div id="roomresults">
<div id="room1"></div>
<div id="room2"></div>
</div>
</div>
<div id="resort2 resortcode2">
<div id="roomresults2">
<div id="room3"></div>
<div id="room4"></div>
</div>
</div>
</div>
This PHP code will group each set of results by resorts, but jacks up my price.
<?php
include("dbh.php");
mysqli_select_db($con, "checkavail") or die ("no database");
$checkin = $_POST['checkin'];
$checkout = $_POST['checkout'];
$occupants = $_POST['occupants'];
$sqlCommand=
"SELECT
MIN(staydate) AS checkin,
MAX(staydate) AS lastnight,
MIN(available) AS available,
ra.resort AS resortcode,
ri.resort,
ri.room,
ri.roomcode,
ri.view,
SUM(ra.points*'20') AS price,
ri.sqfoot,
ri.description,
ri.bedding,
ri.amenities,
ri.sleeps,
ri.sleep_details,
ri.layout_img AS layoutimg,
ri.room_img AS roomimg,
ri.roomimg_thumb
FROM resort_availability AS ra
LEFT JOIN room_info AS ri ON (ra.room = ri.roomcode)
WHERE staydate >= '$checkin' AND staydate = '$checkout'
AND sleeps >= '$occupants'
GROUP BY ri.roomcode
ORDER BY ri.resort, points
";
$result=mysqli_query($con, $sqlCommand) or die(mysqli_error($con));
$data = array();
while($row = $result->fetch_assoc())
{
$itemName = $row["resort"];
$resortCode = $row["resortcode"];
if ( !array_key_exists($itemName, $data)) {
$data[$itemName] = array ();
}
$data[$itemName][] = $row;
}
foreach ($data as $itemName => $rows) {
echo '<div class="resort ' ,$resortCode , '">';
echo '<div class="resort_header"><h1>', $itemName, '</h1></div>';
foreach ($rows as $row){
if ($row['available'] > 0) {
$roomresult = '<div class="room_result roomavailable">';
$available = '<button class="available">BOOK NOW</button>';
$filtavail = 'available';
}
else {
$roomresult = '<div class="room_result roomsoldout" style="">';
$available = '<button class="soldout">SOLD OUT</button>';
$filtavail = 'soldout';
}
echo $roomresult;
echo '<div class="room_thumb"></div>
<div class="room_info"><h4>'
,$row['room'], '-' ,$row['view'] ,
'</h4></div>
<div class="price"><center><h3>','' ,$row['price'],'</h3>' , $available ,'</center></div></div>';
}
echo '</div>';
}
?>
This PHP code gives me accurate results but does not allow me to Create a div with results based on each resort IE, I don't know how to create a Header with the resort name, separating each set of rooms by resort as it is in the first example.
<?php
include("dbh.php");
mysqli_select_db($con, "checkavail") or die ("no database");
$checkin = $_POST['checkin'];
$checkout = $_POST['checkout'];
$occupants = $_POST['occupants'];
$sqlCommand=
"SELECT
MIN(staydate) AS checkin,
MAX(staydate) AS lastnight,
MIN(available) AS available,
ra.resort AS resortcode,
ri.resort AS resort,
ri.room,
ri.roomcode AS roomcode,
ri.view,
SUM(ra.points) AS points,
ri.sqfoot,
ri.description,
ri.bedding,
ri.amenities,
ri.sleeps,
ri.sleep_details,
ri.layout_img AS layoutimg,
ri.room_img AS roomimg,
ri.roomimg_thumb
FROM resort_availability AS ra
LEFT JOIN room_info AS ri ON (ra.room = ri.roomcode)
WHERE staydate >= '$checkin' AND staydate < '$checkout'
AND sleeps >= '$occupants'
GROUP BY ri.resort, roomcode
ORDER BY ri.resort, points
";
$result=mysqli_query($con, $sqlCommand) or die(mysqli_error($con));
while($row = $result->fetch_assoc()) {
$pricecalc = ($row['points'])*20;
$price = number_format($pricecalc, 2);
//RESULTS BOX
if($row['available'] < "1"){
echo '<div id="'.$row['roomcode'].'-'.$row['resortcode'].'"','class="soldout roomresults room'.$row['resortcode'].'">';
}
if($row['available'] > "0"){
echo '<div id="'.$row['roomcode'].'-'.$row['resortcode'].'"','class="available roomresults room'.$row['resortcode'].'">';
}
//IMAGE BOX
echo '<div class="thumbnail"><img height="90" src="',$row['roomimg_thumb'],'">';
echo '</div>';
//ROOM DETAILS
echo '<div class="roomdetails">';
echo '<h4>'.$row['room'].' - '.$row['view'].'</h4>';
echo '<p>Sleeps: '.$row['sleep_details'].'</p>';
echo '<p class="clickfordetails">Click Image For Room Details</p>';
echo '</div>';
//PRICING BOX AND SUBMIT BUTTON
echo '<div class="price">';
echo '<h3>$',$price,'</h3>';
if ($row['available'] < "1"){
echo '<button type="button" class="soldoutbtn">SOLD OUT</button>';
}
if ($row['available'] > "0"){
echo '<button type="button" class="booknowbtn">BOOK NOW</button>';
}
echo '</div,$row>';
echo '</div></div>';
};
?>
Yes group by will give you the 1st record of the same group.
In my understanding , you want to show all the data but place all the records with the same roomcode together.
In that case, please change
GROUP BY ri.roomcode
ORDER BY ri.resort, points
to
ORDER BY ri.roomcode, ri.resort, points

Php while codes show just 1 row

I'm using these codes.
$blog = mysql_query("SELECT * FROM blog ORDER BY id");
while($sutun = mysql_fetch_array($blog)) {
$fake = $sutun["date"];
echo "$fake";
}
When i use echo"$fake"; I can see all of my rows. But when I use <?php echo "$fake" ?> , It shows just 1 row for me.
I want to all of my rows while I'm using <?php echo "$fake" ?>.
Beacuse The echo"$fake"; in with in a loop it will echo at every iteration thats why you can see all your rows but <?php echo"$fake"; ?> executed when the loop is done so only the last row will be echoed;
You should seperate your logic like
<?php
$blog = mysql_query("SELECT * FROM blog ORDER BY id");
while($sutun = mysql_fetch_array($blog)) {
$fake = $sutun["date"];
?>
<?php
echo $fake;
}

how can we handle while loop iteration?

I am having block of code which is working fine but the problem is that...
in one loop it print 'one' qoute.
in 2nd both 'one' and 'two'
in third all three
...so on till the loop execute.
I want to print only one in first time and second qoute in second time and so on till loop executes.
here is my block of code
<div class="records round">
<?php
//show records
$query = mysqli_query($con,"SELECT table2.col2 AS a,table1.col2 AS b, table1.col1 AS c, table1.q_url AS d
FROM {$statement}
LIMIT {$startpoint} , {$limit}");
$output='';
$Authorname='';
$count=1;
while ($row = mysqli_fetch_array($query)) {
$Authorname =$row['a'];
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url=explode('/',$url);
?>
<div class="record round"><?php echo $count; $output .='<a href="http://localhost/quotes/'.$url[5].'/'.$row['d'].'.html">';
echo $output .=$row['b'].'</a>';?></div>
<?php
$count++;
}
?>
</div>
What you are doing here appending string to $output variable so at each iteration its keeping appending.
Your $output variable must be empty before performing new iteration.
Thats the reason you are getting such output.
So to avoid this you have to reinitialize $output variable inside the loop.
Check below code:
while ($row = mysqli_fetch_array($query)) {
$output='';
$Authorname =$row['a'];
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url=explode('/',$url);
?>
<div class="record round"><?php echo $count; $output .='<a href="http://localhost/quotes/'.$url[5].'/'.$row['d'].'.html">';
echo $output .=$row['b'].'</a>';?></div>
<?php
$count++;
}

Echo message based on row number in php

I have a table which displays information from a database. I added a column where I want to display a message according to row number displayed.
<form name="afisare1" method="POST" >
<input type="submit" name="opt1" value="OK"/>
<?php
if (isset($_POST['opt1'])) {
$loc=mysql_query("SELECT loc FROM program WHERE den='Option'");
//$loc result is a number
$sql=mysql_query("SELECT Col1, Col2
FROM date WHERE Opt_1='Option' OR Opt_2='Option'
ORDER BY Col2 DESC");
?>
<table>
<thead>
<tr>
<th>Col1</th> <th>Col2</th> <th>OK/NOT OK</th>
</tr>
</thead>
<?php
$num_rows = 0;
while ($row = mysql_fetch_assoc($sql)) {
$num_rows++;
if ($num_rows <= $loc) {
echo"<tr>
<td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td>
</tr>";
break;
}
if ($num_rows > $locuri_buget) {
//here i have a problem because i don't know how to display something like this :
//echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>NOT OK</td></tr>";
}
}
} ?>
</table>
</form>
For example if the result of $loc=2 i want to echo for the first 2 rows OK and for extra rows i want to echo NOT OK
To retrieve loc from MySQL, your original code does it like this:
$loc = mysql_query("SELECT loc FROM program WHERE den='Option'");
But you might want to change it like this:
$loc = 0; # default value in case of error in a query
$result = mysql_query("SELECT loc FROM program WHERE den='Option'");
while($row = mysql_fetch_assoc($result)){
$loc = $row['loc'];
}
Or if you want retrieve only one record from your databse, you can also write a SQL with LIMIT like this:
$loc = 0; # default value in case of error in a query
$result = mysql_query("SELECT loc FROM program WHERE den='Option' LIMIT 1");
if($row = mysql_fetch_assoc($result)){
$loc = $row['loc'];
}
Hope this helps.
You can try with following inside while loop
if($num_rows<=$loc){
echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td></tr>";
}else{
echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>NOT OK</td></tr>";
}
$num_rows = 0;
while($row = mysql_fetch_assoc($sql)){
$num_rows++;
if($num_rows<=$loc){
echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td></tr>";
}else {
echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td></tr>";
}
}
I think your problem is with break,
break ends execution of the current for, foreach, while, do-while or switch structure.
Break

Active menu class with php function

I'm not getting it done, I need to put a class on the output of the function.
Can you help, when the menu item is active there has to be put a class to the link.
This is the code:
function loadMenu()
{
$result = mysql_query(" SELECT * FROM cms_page WHERE site_id =2 AND page_inmenu =1 AND page_active =1");
if ($DEBUG)
echo "<pre>$result</pre>";
while($row = mysql_fetch_array($result))
{ $last_parent = $row['page_name'];
echo "<a href={$row['link_name']}>{$row['page_name']}</a>";
}
}
function loadMenu()
{
$result = mysql_query(" SELECT * FROM cms_page WHERE site_id =2 AND page_inmenu =1 AND page_active =1");
if ($DEBUG)
echo "<pre>$result</pre>";
while($row = mysql_fetch_array($result))
{
$last_parent = $row['page_name'];
$class = $row['active'] === true ? ' class="active"' : '';
echo "<a href=\"{$row['link_name']}\"{$class}>{$row['page_name']}</a>";
}
}
Also your
I'm not 100% sure about what's your issue.
So, if you aren't displaying anything, probably a MySQL issue.
1- Reference for Mysql (Look at OZ_'s answer) : How to put mysql inside a php function?
2- As mentionned by Beiller, you'll need a way to see if the link is active.
Solution A : Might need some adjustment according $row['link_name'] value.
$class = ($row['link_name'] == $_SERVER['REQUEST_URI'].)?' class="active" ' : '';
Solution B : Simply stick to CSS - http://www.echoecho.com/csslinks.htm
.mylink:active{ background-color:pink; }
3- Href needs quotes on around the URL.
also, I'm not a big fan of {} inside strings.
echo "". $row['page_name'] ."";
Thanks beiller for the help..
This is the function now:
function loadMenu(){
$result = mysql_query(" SELECT * FROM cms_page WHERE site_id =2 AND page_inmenu =1 AND page_active =1");
if ($DEBUG)
echo "<pre>$result</pre>";
while($row = mysql_fetch_array($result))
{ $last_parent = $row['page_name'];
echo "<a href={$row['link_name']}>{$row['page_name']}</a>";
}}
This is the call:
<div id="header_menu">
<? loadMenu (); ?>
</div>
And mysql has the following tables:
page_id
page_name
link_name
site_id
order_id
page_menu
page_inmenu
page_leftmenu
page_text
page_active

Categories