Change class for last two rows in a while loop - php

I have a while loop that displays result from a MySQL query. I know how to change the output for the last row or an odd row but how can I change the output for the last two results?
For example I have a list of results in a 2x2 matrix with border-bottom: 1px on each result but I would like to display the bottom two without the border?

If you can use css3, it´s easy (I´ll use a list for the example):
li:nth-last-child(-n+2)
selects the last two li's.
If you want to do it in php, you can count the number of results, add a counter in your loop and add a class to the last two items.

Yeah just do it like this:
$result = //execute your query
$num_rows = mysql_num_rows($result;
$num_rows_different = 2;
$loop_counter = 0;
while ($loop_counter < $num_rows) {
if ($loop_counter < $num_rows - $num_rows_different) {
// with border
} else {
// no border
}
$loop_counter++;
}
I wouldn't use the CSS3 method due to its poor support...

I like the CSS way, but you'll need to know the total number of items you're listing. Then you can use a simple condition to check for that.
$total_items = [count of items];
$cnt = 0;
while($fetch as $row) {
...
if(++$cnt > ($total_items - 2)) {
// list with no border
} else {
// list with border
}
}

Related

Selecting specific MYSQL rows

I have a page that's suppose to create a Div for every 9 entries in the database.
Every Div will consist of 3 ULs and every UL will consist of 3 LIs.
Something like this:
Demo
So, every LI is where each entry is displayed and every Div is essentially a unique page.
This is my code so far:
$sql = mysql_query("SELECT * FROM `reviews`") or die(mysql_error());
$e_count = mysql_num_rows($sql);
$r_pages = ceil($e_count / 9);
$x = 1;
$y = 1;
if($e_count > 9){ // if there are more than 9 entries in the database
for($x=1;$x<=$r_pages;$x++){ // creates a div for every 9 items in the database
echo '<div class="rp_pages" id="rp_page_'.$x.'">';
for($y=1;$y<=3;$y++){ // creates 3 ULS in each "page"
echo '<ul>';
// 3 lis should appear here
echo '</ul>';
}
echo '</div>';
}
}
The problem is, i don't want to use multiple queries with a LIMIT in them to select the respective entries.
Can this be done with just a single query?
I only see one query (the first select). After that, instead of using it to count the number of rows, parse each row and extract the review text, this way:
Let's imagine that the reviews table has a text column where the review text is saved. Having your code:
$sql = mysql_query("SELECT * FROM `reviews`") or die(mysql_error());
$e_count = mysql_num_rows($sql);
$r_pages = ceil($e_count / 9);
$x = 1;
$y = 1;
if($e_count > 9){ // if there are more than 9 entries in the database
for($x=1;$x<=$r_pages;$x++){ // creates a div for every 9 items in the database
echo '<div class="rp_pages" id="rp_page_'.$x.'">';
for($y=1;$y<=3;$y++){ // creates 3 ULS in each "page"
echo '<ul>';
// Here the code that extract the rows from the query.
for($z=1;$z<=3;$z++){
echo '<li>';
if($data = mysql_fetch_array($sql, MYSQL_ASSOC)) {
echo $data['text']; // Use some type of parser to show special characters like < or > as text, so you are safe from code injection.
}
echo '</li>';
}
echo '</ul>';
}
echo '</div>';
}
}
As you can see, it creates a new loop to create the 3 lis and in each li extracts the text selected in the query, then echoes it.
As http://php.net/manual/en/function.mysql-fetch-array.php says:
Returns an array that corresponds to the fetched row and moves the
internal data pointer ahead.
So the next time you fetch a row, it will be the next one.
PD: PHP's mysql is deprecated. Use mysqli instead. Is the same but with another library that is currently under developement, not like mysql.

PHP/SQL Query find last result

I am running a while loop in PHP selecting data from a mysql database. How can i find out what the last record is,
for example:
$sql="SELECT * from table1 ";
$rs=mysql_query($sql,$conn);
while($result=mysql_fetch_array($rs))
{
echo $result["col1"].' - '.$result["col2"].'<br>';
}
then when it gets to the last record i want to display it like:
echo 'Last Record: '.$result["col1"].' - '.$result["col2"].'<br>';
You basically need to record how many rows you have, and then set up a counter. You can do that using mysql_num_rows():
$sql="SELECT * from table1";
$rs = mysql_query($sql,$conn);
$numRows = mysql_num_rows($rs);
$i = 1;
while($result=mysql_fetch_array($rs))
{
echo ($i == $numRows) ? 'Last Record: '.$result["col1"].' - '.$result["col2"].'<br />' : $result["col1"].' - '.$result["col2"].'<br />';
$i++;
}
You should note though that the mysql_*() family of functions is now deprecated. For security and longevity, you really ought to be using MySQLi or PDO.
Get the total count of rows returned and check use a flag variable for the loop iterations and check in loop if flag == total rows
$t=mysql_num_row($rs);
$i=0;
while($result=mysql_fetch_array($rs))
{
$i++;
if($t == $i){
echo "Last Record ";
}
echo $result["col1"].' - '.$result["col2"].'<br>';
}
mysql_num_rows
You can simply use the sql query itself to get the last value, based on whatever ordering you want (or just use DESC to get the bottom of the natural order):
SELECT * FROM table1
ORDER BY your_column DESC
LIMIT 1;
Edit: Since you're looking for the last row, you could check with mysql_num_rows
$numrows = mysql_num_rows($rs);
$i = 1;
// in while loop...
if ($i === $numrows) {
// print last result
} else {
// print normal result
}
$i++;
// end while loop
Essentially, you want a counter for the record you are on and then write when the number of rows is the same as the row number you are on (e.g. the last one)
$sql="SELECT * from table1 ";
$rs=mysql_query($sql,$conn);
$num_rows = mysql_num_rows ($rs);
for ($i=0; $i < $num_rows; $i++) {
$result=mysql_fetch_array($rs);
if ($i == ($num_rows - 1)) {
echo 'Last Record: '.$result["col1"].' - '.$result["col2"].'<br>';
} else {
echo $result["col1"].' - '.$result["col2"].'<br>';
}
}
Future-proof this routine by doing it the "hard way":
while ($next_row = fetch_row(...)) {
if ($prev_row) { do_output($prev_row); }
$prev_row = $next_row;
}
if ($prev_row) { do_output($prev_row, FLAG_IS_LAST_ROW); }
Why? Future maintenance might make mysql_num_rows() unreliable, either because your result set gets too big, or because you want to interface with a variety of SQL backends.
By default, the MySQL client library pulls the entire result set into memory — that is how it knows the number of rows SELECTed without having to count fetches. This behavior is rather convenient for small result sets, but devastating for large result sets. This it is user-configurable. (The options are usually named something like "store_result v. use_result" or "buffered v. unbuffered.")
Additionally, most RDBMS interfaces do not make the size of the result set known in advance. If you want to interface with these some day in a reusable way, you'll need to change your approach.

How to display data in multiple columns with php?

Hi I need to build a table with two columns from a mySQL table.
Here is what i have now:
<table>
<?php
$sql = "SELECT field1, field2 FROM tblX"
$result = mysql_query($sql) or die("\nError Retrieving Records.");
while($row = mysql_fetch_array ($result, MYSQL_ASSOC)){?>
<tr>
<td>
<?=$row['field1']?> - <?=$row['field2']?>
</td>
</tr>
<?php }?>
</table>
This will create one column table like so:
1
2
3
4
5
I need the table to be in two columns like this:
1 4
2 5
3
Is this possible if it is how do I do that?
This should do what you want, if you insist on a table.
Get the mid point:
$mid = ceil(mysql_num_rows($result)/2);
Get an array:
while( $row = mysql_fetch_row($result) ) {
$list[] = $row; }
Output the rows:
for( $i = 0; $i < $mid; $i++ ) {
$itemOne = $list[$i];
$itemTwo = $list[$i + $mid];
// echo them in two tds.
}
I agree with marco, though. You could just as easily list items one to $mid in one div, then $mid to the end in another, and use CSS to float the divs side-by-side. Using tables for formatting purposes is evil.
read all values in a list, get the half value, write a loop that shows [i] and [i+halfvalue] together
Your question is unclear, but it sounds like you want to rearrange how the data is displayed and don't want the standard row-by-row output. You should probably do this manually if your order requires a method that cannot be looped through.
You should arrange the table BEFORE you output it, then once you've got everything all tidy, simply output the code.
Must it be in tables?
If you have in it a div and you have it set so that 3 items can contain in the height.
<div id="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
</div>
#container { height: 300px; }
.item { height: 100px; }
You can use some css floating to solve that combined with some padding and margin.

PHP while loop to get values from mysql data base if it equals the $i value

I'm trying to get a value to be inserted into a table on a webpage if the value equals $i.
$i starts at a number and decreases every loop. i can get it to work but it outputs multiple lines for each $i equivalent to the results in the table
I've reworked the code using everyones feedback to get this.
Echo "<tr><th colspan='3'><center>$rackname</th> </tr>" ;
for ($i=$RUtotal; $i > 0; $i--)
{
echo" <tr class='rackbg'><td class='i'><center>$i</td>" ;
$sql1 = "SELECT racks.rackID, racks.rackname, devices.deviceID, devices.deviceName, racks.rackRU, devices.deviceRU, devices.RUcount
FROM racks LEFT JOIN devices ON racks.rackID = devices.rackID
WHERE devices.rackID = '$rackID'";
$query1 = mysql_query($sql1);
while ($row = mysql_fetch_assoc($query1))
{
$deviceru = $row['deviceRU'];
$deviceID = $row['deviceID'];
$device = $row['deviceName'];
$deviceRUC = $row['RUcount'];
if ($deviceru == $i)
{
echo '<td class="device" rowspan='.$deviceRUC.'><a onclick=window.location="/devices.php?id='.$deviceID.'">'.$device.'</a></td><td rowspan='.$deviceRUC.'></td></tr>';
}
else
{
;
}
}
}
Echo "<tr class='rackb'><th colspan='3'>a</th></tr> " ;
This works to a degree (picture1) but when i add echo "" to the else statement it displays all wrong. (picture 2)
Any help would be greatly appreciated
Picture1 - http://imageshack.us/photo/my-images/263/examplewq.png/
Picture2 - http://imageshack.us/photo/my-images/269/example2jp.png/
I can't quite see what you're trying to do but what it looks like to me is that you want all the items from racks joined with their relevant device and displayed in order of deviceRU. Does this help:
echo "<tr><th colspan='3'><center><b>$rackname</th></tr>" ;
$sql1 = "SELECT racks.rackID, racks.rackname, devices.deviceID, devices.deviceName, racks.rackRU, devices.deviceRU, devices.RUcount
FROM racks LEFT JOIN devices ON racks.rackID = devices.rackID
WHERE racks.rackID = '$rackID' AND devices.deviceRU <= ".intval($RUtotal)."
ORDER BY devices.deviceRU;"
$query1 = mysql_query($sql1);
while ($row = mysql_fetch_array($query1))
{
$deviceru = $row['deviceRU'];
$deviceID = $row['deviceID'];
$device = $row['deviceName'];
$deviceRUC = $row['RUcount'];
echo'<tr class="rackbg"><td class="i">'.$i.'</td><td class="device">'.$device.'</td><td></td></tr>';
}
I've used a LEFT (inner) JOIN in the SQL instead of the outer join that was there before as it'll return less results and might solve your problem. I've ordered the results by deviceRU and only returned results which have deviceRU less than or equal to $RUtotal (as I think the example was showing).
I've also removed the tags, these should be replaced by using CSS to centre either all td elements or centering class="device" and class="i" e.g.:
.device, .i {
text-align: center;
}
I've also swapped your abc to abc which is the correct format for a link.
Could you describe more of the context as it's difficult to see your intention from your post.
Mat
As Peetz said, you don't need nested loop. You need something like:
$i = $RUtotal;
// ...
while ($row = mysql_fetch_array($query1)) {
// ...
if ($deviceru == $i) {
// ...
} else {
// ...
}
// ...
$i--;
}
This is looping $i times, within the outer while loop. This means you are getting the table repeated over and over again.
I suggest you remove the outer while loop.

Split records into two columns

I have a "student" table, having around 5,000 records, in my DB. I want to display those records in two divs. How do I do that without executing the query twice; only using a single query?
display example http://www.freeimagehosting.net/uploads/f1c6bb41eb.gif
Just use CSS3. Not sure how widely it is supported but saves a lot of headache and is a lot more powerful when making changes.
Use column-count, and column-width to control the number of columns and width of each column. Here's some sample code and some pretty impressive results. Prefix -webkit and -moz for now until its standardized across all browsers.
.multi-column {
/* Standard */
column-count: 2;
column-width: 150px;
/* Webkit-based */
-webkit-column-count: 2;
-webkit-column-width: 150px;
/* Gecko-based */
-moz-column-count: 2;
-moz-column-width: 150px;
}
Applied to this <div>
<div class="multi-column">
Ethelred of Wessex
Louis XII of France
George Frideric Handel
George Washington
Charles Deslandes
Andrew Jackson
Alfred Vail
William McKinley
Woodrow Wilson
Abdul-Aziz ibn Saud
Fidel Castro
Charles de Gaulle
Leonardo da Vinci
</div>
Don't you wanna see how it looks like after all this hard work?
But what if there were 3 columns? No problem.
But there's no way it can handle 4 columns you'd say:
Enough! I gotta stop adding these now
God please make it STOP!!
Just find where the "middle" is and output the end tag of the div tag and the start tag of the second div:
<?
$rowcount = mysql_num_rows($recordset);
echo "<div id='div1'>";
$i = 0;
while ($d = mysql_fetch_object($recordset)) {
echo $d->somefield;
$i++;
if ($i == floor($rowcount / 2)) {
//we have reached the mid-point, let's close the first DIV
echo "</div><div id='div2'>";
}
}
echo "</div>";
?>
get_column() function could help, it will calculate column for each item you want to show.
this sample script show how to print in two columns, but you can change it in two minutes to fix your needs if you need other number of columns.
<?php
// sample query, get members
$query = mysql_query("select name from persons");
// count total number of items to show
$total = mysql_num_rows($query);
// initiate row counter
$counter = 1;
// initiate columns contents
$column_1 = '';
$column_2 = '';
while($row = mysql_fetch_assoc($query)){
// caluculate column for current element from total items to be showed number of columns and current item
$column = get_column($total, 2, $counter);
if($column == 1){
$column_1 .= $row['name'].'<br>';
}
if($column == 2){
$column_2 .= $row['name'].'<br>';
}
$counter++;
}
// show content in two table comments
echo "<table>
<tr>
<td>$column_1</td>
<td>$column_2</td>
</tr>
</table>";
?>
and the function is:
<?php
/**
* Calculate column number where an item should be displayed on a "newspaper style"
* or "phoneguide style" report according its postion
* used to put same number of items on each column
*
* receive 3 numbers: $vp_total_size: total number of items on report
* $vp_columns : number of columns of report
* $vp_element : element position for item (1 to $vp_total_size)
*
* by Marcos A. Botta <marcos DOT botta AT gmail DOT com>
* 02/02/2007
*
*/
function get_column($vp_total_size, $vp_columns, $vp_element){
if($vp_element <= 0){
return 1;
}
if($vp_element < $vp_columns &&
$vp_columns >= $vp_total_size){
return $vp_element;
}
$vl_avg_items_by_column = $vp_total_size / $vp_columns;
$vl_items_on_first_columns = ceil($vl_avg_items_by_column);
$vl_items_on_last_columns = floor($vl_avg_items_by_column);
$vl_column_limit = ($vl_avg_items_by_column - $vl_items_on_last_columns) * $vp_columns;
$vl_allocated_items = 0;
for($i=1;$i<$vp_columns;$i++){
if($i < $vl_column_limit ||
"$i" == "$vl_column_limit"){
$vl_items_on_current_column = $vl_items_on_first_columns;
}
else{
$vl_items_on_current_column = $vl_items_on_last_columns;
}
$vl_allocated_items += $vl_items_on_current_column;
if($vp_element <= $vl_allocated_items){
return $i;
}
}
return $vp_columns;
} // get_column()
?>
good luck!
My implementation:
<?php
$students = array(1,2,3,4,5);
$split = floor(count($students)/2);
echo '<div id="parent"><div id="col-1">';
$i = 0;
foreach($students as $student)
{
echo 'Student #' . $student . '<br />';
if($i == $split)
{
echo '</div><div id="col-2">';
}
$i++;
}
echo '</div></div>';
Using the CSS3 Webkit/Moz only features are in my opinion very bad practice.
Why dont you try this code, its simple using only css, easy to understand, working in ie and mozilla...
<style type="text/css">
.ulcol
{
float: left;
width: 400px;
margin: 0;
padding: 0;
list-style: none;
}
.licol
{
float: left;
width: 200px; /*half width of the ulcol width*/
margin: 0;
padding: 0;
}
</style>
<?php
$query = mysql_query("select * from table_name") or die("Error Occured,check again");
echo '<ul class="ulcol">';
while($row = mysql_fetch_assoc($query))
{
$vartitle = $row[db_row_title];
echo '<li class="licol">';
echo $vartitle
echo '</li>';
}
echo '</ul>';
?>
Maybe split the array into two then implode them and then print both halves on each div.
I prefer to minimize any early use of "echo", because tomorrow you will want to move this in a function or a method, where "echo" should be avoided. Moreover, with the "echo" in the loop you've lost the array structure inherent to databases, and you need this structure to manipulate your data. So I would rather fill an array, process it, then implode to output.
And I would use styled bullet points to display the items, because you apparently want to display a list of items. In pseudo php code:
while row = fetch(sql)
lines[] = "<li>row_data</li>"
end
// work on the lines array, eg insert "</ul><ul>" in the middle
echo "<ul>".implode("\n",lines)."</ul>"
Try something like this
// connection goes there
$q = "SELECT `name` FROM students";
$result = mysql_query($q);
$students = array();
while($row=mysql_fetch_assoc($result)) {
$students[] = $row['name'];
}
$students_count = sizeof($students);
// chunkes into a two parts , want more columns ? just change "2" to other number
$students_chuncked = array_chunk($students,ceil($students_count/2),true);
//now display
foreach ($students_chuncked as $student_div_data){
echo '<div>',explode($student_div_data,'<br/>'),'</div>';
}

Categories