PHP Mysql - adding an empty row can't figure it out - mysql

Here is the table code - when this is printed out the has one empty row at the bottom. I am not sure why the last row is added? Is it perhaps the if else statement?
<?php
$row = mysql_fetch_array($result);
if ($row['Price'] == '0.00') {
echo "<TABLE width='100%'>\n";
echo "<TR><TD id=tblrowsHead2>Item</TD><TD id=tblrowsHead2>Size</TD><TD
id=tblrowsHead2>Contact</TD></TR>\n";
for ($i = 0; $i < $numofrows; $i++) {
$row = mysql_fetch_array($result); //get a row from our result set
if ($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#E6E6FA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo "<TD id=Size>" . $row['Item'] . "</TD><TD id=Size>" . $row['Size']
. "</TD>
<TD id=Price>Contact Us</TD>\n";
}
} else {
echo "<TABLE width='100%'>\n";
echo "<TR><TD id=tblrowsHead2>Item</TD><TD id=tblrowsHead2>Size</TD><TD
id=tblrowsHead2>Price</TD></TR>\n";
for ($i = 0; $i < $numofrows; $i++) {
$row = mysql_fetch_array($result); //get a row from our result set
if ($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#E6E6FA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo "<TD id=Size>" . $row['Item'] . "</TD><TD id=Size>" . $row['Size']
. "</TD>
<TD id=Price>$" . $row['Price'] . "</TD>\n";
}
}
//now let's close the table and be done with it
echo "</TABLE>\n";
?>

Try this code instead:
<?php
$row = mysql_fetch_array($result);
$i = 0;
if ($row['Price'] == '0.00') {
echo "<TABLE width='100%'>\n";
echo "<TR><TD id=tblrowsHead2>Item</TD><TD id=tblrowsHead2>Size</TD><TD id=tblrowsHead2>Contact</TD></TR>\n";
do {
if ($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#E6E6FA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo '<TD id=Size>'.$row['Item'].'</TD><TD id=Size>'.$row['Size'].'</TD>';
echo "<TD id=Price>Contact Us</TD></TR>\n";
$i++;
} while ($row = mysql_fetch_array($result)); //get a row from our result set
} else {
do {
if ($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#E6E6FA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo '<TD id=Size>'.$row['Item'].'</TD><TD id=Size>'.$row['Size'].'</TD>';
echo "<TD id=Price>$" . $row['Price'] . "</TD></TR>\n";
$i++;
} while ($row = mysql_fetch_array($result)); //get a row from our result set
}
//now let's close the table and be done with it
echo "</TABLE>\n";
When you using the mysql_fetch_array at the first line of for-loop, you will lost the first record of your data-table, so you will have an empty line at the end

Related

How I add a class css to php?

function autoDate()
{
for ($i = 1; $i <= date("t"); $i++) {
echo "<li>" . $i . "</li>";
if ($i == date("j")) {
???
}
}
}
I need to put class "active" when date of today is true...
If you are intending to add the active class to the list, then you should do that before you echo your list:
function autoDate() {
for ($i = 1; $i <= date("t"); $i++) {
echo "<li" . ($i == date("j") ? ' class="active"' : '') . ">" . $i . "</li>";
}
}
I think you should code here:
function autoDate() {
$data = "";
for ($i = 1; $i <= date("t"); $i++) {
$data .= "<li>" . $i . "</li>";
if ($i == date("j")) {
$data .= "<li class='abc' >" . $i . "</li>";
}
}
echo $data;
}
function autoDate()
{
for ($i = 1; $i <= date("t"); $i++) {
$class = '';
if($i == date("j")){
$class = 'active';
}
echo "<li class='".$class."'>" . $i . "</li>";
}
}
Try this one.
by default $class is empty and if date matched then $class = 'active'

Trying to echo a table but it's not working

The table is called bookings. I'm trying to echo rows from the table, but I'm getting errors in some places according to user_id. Dreamweaver tells me the last few lines have errors, but I'm not sure what's the problem.
<?php
require 'connect.php';
include("auth.php");
$user = $_SESSION['username'];
$uid = $_SESSION['user_id'];
$sql = "SELECT * FROM bookings WHERE user_id =". $uid;
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) { // if the mysqli_query performed above works
echo "<br>";
echo '<table border="1">';
echo '<tr>
<th>booking_id</th>
<th>"Room_name"</th>
<th>"booking_date"</th>
<th>"period_name"</th>
<th>"booking_id"</th>
<th>"Cancel Booking"</th>
</tr>';
while ($row = mysqli_fetch_assoc($result)) { // important line
echo '<tr>';
echo '<td>'.$row["booking_id"].'</td>';
echo '<td>'.$row["Room_name"].'</td>';
echo '<td>'.$row["booking_date"].'</td>';
echo '<td>'.$row["date_booked"].'</td>';
echo '<td>'.$row["period_name"].'</td>';
echo '<td>'.$row["booking_id"].'</td>';
echo "</tr>";
echo "</table>";
}
else echo '<p>You have no bookings.</p>';
I have made some change
And here is the code-
require 'connect.php';
include 'auth.php';
$user = $_SESSION['username'];
$uid = $_SESSION['user_id'];
$sql = "SELECT * FROM bookings WHERE user_id ='". $uid."'";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) { // if the mysqli_query performed above works
echo "<br>";
echo '<table border="1">';
echo '<tr>
<th>booking_id</th>
<th>Room_name</th>
<th>booking_date</th>
<th>period_name</th>
<th>booking_id</th>
<th>Cancel Booking</th>
</tr>';
while ($row = mysqli_fetch_assoc($result)) { // important line
echo '<tr>';
echo '<td>'.$row["booking_id"].'</td>';
echo '<td>'.$row["Room_name"].'</td>';
echo '<td>'.$row["booking_date"].'</td>';
echo '<td>'.$row["date_booked"].'</td>';
echo '<td>'.$row["period_name"].'</td>';
echo '<td>'.$row["booking_id"].'</td>';
echo '</tr>';
}
echo '</table>';
} else echo '<p>You have no bookings</p>';

adding different class/style to rows that variable fulfills the condition

The problem is that all images are displayed with opacity: 0.4. I need different style to each row that value from number column is less than 100.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$progres = $row['number'];
$num_rows++;
echo "$progres <br/>";
echo "<div class='right_achiev'>";
echo '<div id="box1" class="box">';
echo '<span class="caption simple-caption">';
echo '<p class="sq1">'.$row['name'].'</p>';
echo '<p class="unlok">UNLOCKED:<br/> 2014-01-09 16:25 </p>';
echo '</span>';
if ($progres < 100)
{
echo "<img class='achiev_icon' src='".$row['icon']."'/>";
}
else
{
echo "<img class='achiev_icon' style='opacity:0.4' src='".$row['icon']."'/>";
}
echo "</div>";
echo "</div>";
}
you can try to use this code
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$progres = intval($row['number']);
$num_rows++;
echo "$progres <br/>";
echo "<div class='right_achiev'>";
echo '<div id="box1" class="box">';
echo '<span class="caption simple-caption">';
echo '<p class="sq1">'.$row['name'].'</p>';
echo '<p class="unlok">UNLOCKED:<br/> 2014-01-09 16:25 </p>';
echo '</span>';
if ($progres < 100)
{
echo "<img class='achiev_icon' src='".$row['icon']."'/>";
}
else
{
echo "<img class='achiev_icon' style='opacity:0.4' src='".$row['icon']."'/>";
}
echo "</div>";
echo "</div>";
}

Php mysql fetch result

I've made a script for movies but i have an issue.
The home of script is contain code like this :
<div class="box">
<div id="movie"></div><div id="movie"></div><div id="movie"></div>
</div>
I've do a mysql query but i don't know how to echo just 3 record per div .
Im using this code for mysql query :
$query=mysql_query("select * from movies where id=$id");
while($row=mysql_fetch_assoc($query))
{
echo $row['name'];
}
You will need something like this I think
$query = mysql_query("select * from movies");
$result = array();
while($r = mysql_fetch_assoc($query)) {
$result[$r['id']] = array($r['name'],$r['thumb']);
}
$i = 0;
foreach($result as $id => $data){
if($i == 0)
{
echo "<div class=\"box\">";
echo "<div id=\"movie\">";
echo "ID: $id";
echo "Name: $data[0]";
echo "Thumb: $data[1]";
echo "</div>";
$i = $i + 1;
}
elseif($i == 1)
{
echo "<div id=\"movie\">";
echo "ID: $id";
echo "Name: $data[0]";
echo "Thumb: $data[1]";
echo "</div>";
$i = $i + 1;
}
elseif($i == 2)
{
echo "<div id=\"movie\">";
echo "ID: $id";
echo "Name: $data[0]";
echo "Thumb: $data[1]";
echo "</div>";
echo "</div>";
$i = 0;
}
}
Multiple elements with the same id? Not a good idea.
You didn't say what you want to happen if you're query returns less then 3 rows.
You might try:
...
$x=0;
while($row=mysql_fetch_assoc($query) && ++$x<=3) {
echo $row['name'];
}

PHPEXCEL mysql php insert empty rows

I am trying to use phpexcel to let users upload a spreadsheet and insert into mysql. I have it working except if the user has blank rows in the spreadsheet it puts several unnecessary rows in the database. I have read and mysql doesnt support check constraints and most of that would need to be done at the application level which is what I am trying to do. How in PHP could have it check if a specific column is empty and skip that row. Any help would be appreciated or if there is a better solution I am up for all options. I was trying for a while to get it that is the bldg column was empty than just skip that row but wasnt having any luck..
<?php require '../Classes/PHPExcel.php';
require_once '../Classes/PHPExcel/IOFactory.php';
$path = "../upload/uploads/$Filename";
$objPHPExcel = PHPExcel_IOFactory::load($path);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow();
$highestColumn = 'L'; // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;
$objReader = PHPExcel_IOFactory::createReaderForFile($path);
$objReader->setReadDataOnly(true);
echo '<br>Data: <table width="100%" cellpadding="3" cellspacing="0"><tr>';
for ($row = 16; $row <= $highestRow; ++ $row) {
echo '<tr>';
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val = $cell->getValue();
if($row === 1)
echo '<td style="background:#000; color:#fff;">' . $val . '</td>';
else
echo '<td>' . $val . '</td>';
}
echo '</tr>';
}
echo '</table>';
for ($row = 16; $row <= $highestRow; ++ $row) {
$val=array();
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val[] = $cell->getValue();
}
$sql="insert into excess1(date_excessed, bldg, floor, jack, equipment_owner, contact_name, contact_phone, qty, type_of_excess, asset_tag, service_tag, comments)
values('".$val[0] . "','" . $val[1] . "','" . $val[2]. "','" . $val[3]. "','" . $val[4]. "','" . $val[5]. "','".$val[6] . "','".$val[7] . "','".$val[8] . "','".$val[9] . "','".$val[10] . "','".$val[11] . "')";
$result = mysql_query($sql) or die(mysql_error());
print "$result";
}
}
unlink("../upload/uploads/$Filename");
?>
</article>
$inputFileType = PHPExcel_IOFactory::identify($filename);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($inputFileName);
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
$i=0;
foreach ($allDataInSheet as $value) {
if ($value === null){
continue;
} //this is skipping empty rows
$data[$i] = $value['A'];
$data[$i] = $value['B'];
$i++;
}