PHP Nesting do-while loop - php

Im trying to teach myself php.... I have created a database with 4 elements... region, details, store_name, web_address.
I have created ado-while loop that displays all the records in a 'region'... it displays 'store_name', then 'web_address', then 'details' sorted so all the records with the same 'details' are grouped together;
<table width="600" align="center" border="0" cellspacing="4" >
<tr>
<td colspan="2"><h2>Web Stockists: <strong></strong></h2></td>
<td width="251" colspan="2" align="right"><h3>Region: <?php echo $region_text ?></h3></td>
</tr>
<?php do { ?>
<tr>
<td colspan="2"><strong><?php echo $row_RegionList['store_name']; ?></strong></td>
<td colspan="2" rowspan="2" align="center"> <?php echo '' . $row_RegionList['web_address'] . ''; ?></td>
</tr>
<tr>
<td width="14">
<td width="313"><?php echo $row_RegionList['details']; ?> </tr>
<?php } while ($row_RegionList = mysql_fetch_assoc($RegionList)); ?>
</table>
I now want to make it so every record with the same 'details' value get listed under a 'details' sub heading.
This is my attempt;
<table width="600" align="center" border="0" cellspacing="4" >
<tr>
<td colspan="2"><h2>Web Stockists: <strong></strong></h2></td>
<td width="251" colspan="2" align="right"><h3>Region: <?php echo $region_text ?></h3></td>
</tr>
<?php $details = $row_RegionList['details'];
do { ?>
<tr>
<td width="313"> <h3><?php echo $row_RegionList['details']; ?> </h3></td>
</tr>
<?php do { ?>
<tr>
<td colspan="2"><strong><?php echo $row_RegionList['store_name']; ?></strong></td>
<td colspan="2" rowspan="2" align="center"> <?php echo '' . $row_RegionList['web_address'] . ''; ?></td>
</tr>
<?php } while ($row_RegionList['details'] == $details);
$details = $row_RegionList['details'];
} while ($row_RegionList = mysql_fetch_assoc($RegionList)); ?>
</table>
This makes a heading of 'region' with a sub heading of 'details' but then creates an eternal loop of the first record in the database. can someone show me what I have done wrong?

Related

Table inside nested Table

I am having a nested table with a while loop, I want to add one more nested table in the same row:
Now I want to add one more nested table as each cd contains more than one data like below:
My code is as follows
<?php
if(isset($_POST['viewcd'])){
$queryw = "select * from lib_cd where id=".$_POST['idd'];
$resultw = $mysqli->query($queryw);
?>
<div>
<table border="1">
<thead>
<tr ><th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th>CD No:</th>
<th >Logs</th>
</tr>
</thead>
<?php
while($rowcd = $resultw->fetch_assoc()){
?>
<tr>
<td><?php echo $rowcd['id'] ?> </td>
<td><?php echo $rowcd['well_no'] ?></td>
<td><?php echo $rowcd['well_name'] ?></td>
<td>
<table border="1" width="100%">
<?php
$querycd = "select * from cd where pidd=".$rowcd['id'];
$resultcd = $mysqli->query($querycd);
while($rowcd = $resultcd->fetch_assoc()){
?>
<tr>
<td ><?php echo $rowcd['cd_no'] ?></td>
/* I want to add one more nested table here*/
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
}
?>
</table>
</div>
I tried some thing like this,after my second while loop
while($rowcd = $resultcd->fetch_assoc()){
?>
<tr>
<td ><?php echo $rowcd['cd_no'] ?></td>
<td>
<table>
<?php
$queryl = "select * from lib_cd_logs where pid=".$rowcd['cd_no'];
$resultl = $mysqli->query($queryl);
while($rowl = $resultl->fetch_assoc()){
?>
<tr>
<td><?php echo $rowl['logs'] ?></td>
</tr>
<?php
}
?>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
}
?>
</table>
</div>
but the result was messed up. I am confused, where I want to end my while loop, I think.
Finally i got as i wish, and i am sharing the code as below
<?php
if(isset($_POST['viewcd'])){
$queryw = "select * from lib_cd where id=".$_POST['idd'];
$resultw = $mysqli->query($queryw);
?>
<div class="container">
<table border="1" align="center" border-collapse="collapse">
<thead>
<tr >
<th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th width="100">CD No:</th>
<th width="150">Logs</th>
<th width="100">Bottom Depth</th>
<th width="100">Top Depth</th>
<th width="100">Date of Log</th>
</tr>
</thead>
<?php
while($rowcd = $resultw->fetch_assoc()){
?>
<tr>
<td><?php echo $rowcd['id'] ?> </td>
<td align="center"><?php echo $rowcd['well_no'] ?></td>
<td align="center"><?php echo $rowcd['well_name'] ?></td>
<td colspan="5">
<table rules="all">
<tr>
<?php
$querycd = "select * from cd where pidd=".$rowcd['id'];
$resultcd = $mysqli->query($querycd);
while($rowcd = $resultcd->fetch_assoc()){
?>
<td width="100" align="center"><?php echo $rowcd['cd_no'] ?></td>
<td colspan="4">
<table rules="all">
<tr>
<?php
$queryl = "select * from lib_cd_logs where pid=".$rowcd['cd_no'];
$resultl = $mysqli->query($queryl);
while($rowl = $resultl->fetch_assoc()){
?>
<td width="155"><?php echo $rowl['logs'] ?></td>
<td width="105" align="center"><?php echo $rowl['bottom'] ?></td>
<td width="100" align="center"><?php echo $rowl['top'] ?></td>
<td width="100" align="right"><?php echo $rowl['date'] ?></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
?>
</table>
</td>
<?php
}
}
?>
</tr>
</table>
I hope this is what you meant as per your data table shown above
<div>
<table border="1">
<thead>
<tr ><th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th>CD No:</th>
<th >Logs</th>
</tr>
</thead>
<tr>
<td>id</td>
<td>well</td>
<td>name</td>
<td>
<table border="1" width="100%">
<tr>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
</table>
</td>
<td>
<table border="1" width="100%">
<tr>
<td>Log1</td>
</tr>
<tr>
<td>Log2</td>
</tr>
</table>
</td>
</tr>
</table>
</div>

store part of the page in variable

i have a page contains a table and this table contains a lot of coding like this
<table class="table">
<tbody>
<tr >
<td width="20" class="tabletop">م</td>
<td class="tabletop" >name</td>
<td class="tabletop" style="width:120px">date</td>
<td class="tabletop" style="width:120px">note1</td>
<td class="tabletop" style="width:100px">note2</td>
<td class="tabletop" style="width:90px">note3</td>
</tr>
<? $res=mysql_query($sql);
while($resArr=mysql_fetch_array($res)){ ?>
<tr style="width:700px">
<td class="tabletext"><?= ++$serial;?></td>
<td class="tabletext" ><?= $resArr[stName];?></td>
<td class="tabletext"><?= $resArr['date'];?></td>
<td class="tabletext" ><?= $resArr[matName];?></td>
<td class="tabletext" ><? if($resArr[exam]==1) echo "work";else echo "final";?></td>
<td class="tabletext" ><? if($resArr[exam_type]==1) echo "prac";else echo "test";?></td>
</tr>
<? }?>
</tbody>
</table>
as you see the table has php coding
now i want to store the whole table in variable so i can send it to pdf printing library tcpdf
You can use heredoc syntax, but you need to move the conditionals outside:
<?php
if($resArr['exam']==1) $exam = "work"; else $exam = "final";
if($resArr["exam_type"]==1) $examtype = "prac";else $examtype = "test";
$var = <<<EOT
<table class="table">
<tbody>
<tr >
<td width="20" class="tabletop">م</td>
<td class="tabletop" >name</td>
<td class="tabletop" style="width:120px">date</td>
<td class="tabletop" style="width:120px">note1</td>
<td class="tabletop" style="width:100px">note2</td>
<td class="tabletop" style="width:90px">note3</td>
</tr>
$res=mysql_query($sql);
while($resArr=mysql_fetch_array($res)){
<tr style="width:700px">
<td class="tabletext">{++$serial}</td>
<td class="tabletext" >{$resArr["stName"]}</td>
<td class="tabletext">{$resArr["date"]}</td>
<td class="tabletext" >{$resArr["matName"]}</td>
<td class="tabletext" >{$exam}</td>
<td class="tabletext" >{$examtype}</td>
</tr>
<? }?>
</tbody>
</table>
EOT;
echo $var;
Another way, if you already have all of this code and want to save it:
Before the table:
<?php ob_start(); ?>
After the table:
<?php $output = ob_get_contents(); ?>
The table will still be displayed and you can use $output to send to the PDF.

display text with if/else statement if value not found in mysql table?

I'm trying to find a way of displaying an else or if statement in this code so that if the record/value doesn't exist in the mysql table then it will echo a piece of text like 'ask me'.
heres the code:
<?php
$rates_set = get_rates();
while ($rates = mysql_fetch_array($rates_set)) {
?>
<table width="110%" border="0" cellspacing="5" cellpadding="5">
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="col">Rates</th>
<th align="center" valign="middle" bgcolor="#EBEBEB" scope="col">money in</th>
<th align="center" valign="middle" bgcolor="#EBEBEB" scope="col">money Out</th>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">cost</th>
<td align="center" valign="middle">£<?php echo "{$rates['labour']} "; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['material']}"; ?></td>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">cost/th>
<td align="center" valign="middle">£<?php echo "{$rates['money']}"; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['expense']}"; ?></td>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">Overnight</th>
<td align="center" valign="middle">£<?php echo "{$rates['charges']}"; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['fees']}"; ?></td>
</tr>
</table>
<?php } ?>
hope someone can help. thank you.
Try this,
<?php echo isset($rates['fees'])?"{$rates['fees']}":"ask me"; ?>
You can loop through the values in the $rates array and if empty, assign a default:
$rates_set = get_rates();
while ($rates = mysql_fetch_array($rates_set)) {
// Set defaults
foreach($rates as $key => $value)
{
if(strlen(trim($value)) == 0)
{
// Assign default
$rates[$key] = 'Ask Me';
}
}
?>
There is some tweaking to be done with this method, such as how you determine a value is "empty"...but essentially, this will get the job done.
The other option is to handle it locally within the markup using the ternary operator:
<td align="center" valign="middle">£
<?php isset($rates['money']) ? $rates['money'] : 'Ask Me'; ?>
</td>
<?php
/* Use This */
$rates_set = get_rates();
if (mysql_num_rows($rates_set)>0) {
while ($rates = mysql_fetch_array($rates_set)) {
?>
<table width="110%" border="0" cellspacing="5" cellpadding="5">
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="col">Rates</th>
<th align="center" valign="middle" bgcolor="#EBEBEB" scope="col">money in</th>
<th align="center" valign="middle" bgcolor="#EBEBEB" scope="col">money Out</th>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">cost</th>
<td align="center" valign="middle">£<?php echo "{$rates['labour']} "; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['material']}"; ?></td>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">cost/th>
<td align="center" valign="middle">£<?php echo "{$rates['money']}"; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['expense']}"; ?></td>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">Overnight</th>
<td align="center" valign="middle">£<?php echo "{$rates['charges']}"; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['fees']}"; ?></td>
</tr>
</table>
<?php }
} else {
echo 'Ask me';
}
?>
You can use this
<?php
$rates_set = get_rates();
if(mysql_num_rows($rates_set)<1) {
echo "Ask me";
}else{
while ($rates = mysql_fetch_array($rates_set)) {
//your code
}
}
<?php echo ( (!empty($rates['labour'])) ? $rates['labour'] : 'Default Text'); ?>
Is that what you are looking for?
Edit:
Alter the top of the loop like this:
while ($rates = mysql_fetch_array($rates_set)) {
if (empty($rates_set)) continue;

Multidimensional Arrays two loops

MSSQL query link is this;
click here for picture
and my output shows three different pictures for the same product as shown below. What I want is, if the product is the same, keep just one picture and then get the colors and sizes for that product.
Means;
My output ıs the picture below,
click here for picture
as you see there are three product in the picture but they are the same product with different colors and sizes, instead of seeing the same product every time, I want my output like in the picture below.
<table width="376" cellspacing="0" class="stats" width:100%>
<tr>
<td colspan="9" align="center"><?php echo $secim ?></td>
</tr>
<?php
while(odbc_fetch_into($sql_result, &$row)) {
$unit1 = floor($row[3]);
$unit2 = floor($row[4]);
$unit3 = floor($row[5]);
$unit4 = floor($row[6]);
$unit5 = floor($row[7]);
?>
<tr>
<td colspan="2" align="left" valign="top"><?php echo"$row[0]";?></td>
<td>36</td>
<td>38</td>
<td>40</td>
<td>42</td>
<td>44</td>
</tr>
<tr>
<td width="114" align="right" valign="top">
<img src= <?php echo"images/Resize/$row[2]"?>></td>
<td width="25" valign="top"><?php echo"$row[1]";?></td>
<td width="25"valign="top"><?php echo"$unit1";?></td>
<td width="25"valign="top"><?php echo"$unit2";?></td>
<td width="25"valign="top"><?php echo"$unit3";?></td>
<td width="25"valign="top"><?php echo"$unit4";?></td>
<td width="25"valign="top"><?php echo"$unit5";?></td>
</tr>
<?php } }?>
<?php
odbc_free_result($sql_result);
odbc_close($connection);
?>
</table>
I think this is what you are looking for, http://jsfiddle.net/sv8ZS/
while(odbc_fetch_into($sql_result, &$row)) {
$unit1 = floor($row[3]);
$unit2 = floor($row[4]);
$unit3 = floor($row[5]);
$unit4 = floor($row[6]);
$unit5 = floor($row[7]);
?>
//you can check with the index to see if its a first row or not
//This will avoid printing the same header for each row.
if (this-is-the-first row) {
<tr>
<td colspan="2" align="left" valign="top"><?php echo"$row[0]";?></td>
<td>36</td>
<td>38</td>
<td>40</td>
<td>42</td>
<td>44</td>
</tr>
}
//above if ends
<tr>
//If its not a first row then do not show the image again and merge all the next rows
if (this-is-the-first-row) {
<td width="114" align="right" valign="top" rowspan='3'>
<img src= <?php echo"images/Resize/$row[2]"?>
</td>
}
//above if ends
<td width="25" valign="top"><?php echo"$row[1]";?></td>
<td width="25"valign="top"><?php echo"$unit1";?></td>
<td width="25"valign="top"><?php echo"$unit2";?></td>
<td width="25"valign="top"><?php echo"$unit3";?></td>
<td width="25"valign="top"><?php echo"$unit4";?></td>
<td width="25"valign="top"><?php echo"$unit5";?></td>
</tr>
<?php } }?>
enter code here
<?php
echo "<table border='1'>
<tr>
<th>Model</th>
<th>Color</th>
<th>Unit</th>
</tr>";
?>
<?php
while($row =odbc_fetch_array($result)){
//if $row['sAciklama']=$row['sAciklama'] bla bla ???
if (this-is-the-first row) {
echo "<tr>";
echo "<td>" . $row['sAciklama'] . "</td>"; //Model First Element
}
if (this-is-the-first-row) {
echo "<td>" . $row['sRenkAdi'] . "</td>"; //color
echo "<td>" . $row['Kalan'] . "</td>"; //unit
echo "</tr>";
}}
echo "</table>";
?>

order by date mysql query

I used the following code to display the event details fetched from the database.
<form name="event_form" id="event_form" action="" method="post" enctype="application/x-www-form-urlencoded">
<table width="765" border="0" align="left" cellpadding="0" cellspacing="0" >
<tr>
<td>
<table width="765" border="0" align="left" cellpadding="0" cellspacing="0" id="results" class="fronttbl">
<tr></tr>
<?php
$select = "SELECT * FROM `tbl_event`";
$select_event = mysql_query($select);
$select_num = mysql_num_rows($select_event);
if($select_num > 0)
{
while($fetch = mysql_fetch_array($select_event))
{
$feventid=$fetch['intEventid'];
$feventname=stripslashes(ucfirst($fetch['varEventname']));
$fDate=$fetch['varDate'];
$seperate=explode("-", $fDate );
$year=$seperate[0];
$month=$seperate[1];
$date=$seperate[2];
$fchiefguest=stripslashes(nl2br($fetch['varChiefguest']));
$fvenue=stripslashes($fetch['varVenue']);
$ftime=stripslashes($fetch['varTime']);
$feventdetails=stripslashes($fetch['varEventdetails']);
echo " ";
?>
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="30%" height="30" valign="top"><strong>Name of the event:</strong></td>
<td width="70%" height="30" valign="top"><?php echo $feventname; ?></td>
</tr>
<tr>
<td height="30" valign="top"><strong>Date of the event to be held:</strong></td>
<td height="30" valign="top"><?php echo $date.'-'.$month.'-'.$year; ?></td>
</tr>
<tr>
<td height="30" valign="top"><strong>Time of the Event:</strong></td>
<td height="30" valign="top"><?php echo $ftime; ?></td>
</tr>
<tr>
<td height="30" valign="top"><strong>Venue of the event:</strong></td>
<td height="30" valign="top"><?php echo $fvenue; ?></td>
</tr>
<tr>
<td height="30" valign="top"><strong>Name of the Chief Guest:</strong></td>
<td height="30" valign="top"><?php echo $fchiefguest; ?></td>
</tr>
<tr>
<td height="30" valign="top"><strong>Event Details:</strong></td>
<td height="30" valign="top"><?php echo $feventdetails; ?></td>
</tr>
</table>
<p style="border-bottom:1px dotted #CCCCCC;"></p>
</td></tr>
<?php }
}
?>
</table>
</td>
</tr>
</table>
<div id="pageNavPosition"></div>
</form>
I have date field in my database table and the input will be saved like the following format 2012-03-01.
I need my page displays the events order by recent date/month/year in the frontend. For example today's event should displayed first likewise. How can i do that?
Use ORDER BY (...is that what you meant)?
Change your query to:
SELECT *
FROM `tbl_event`
ORDER BY varDate DESC
as i understand use this function for front end:
function changeFormat( $date ){
$exp_date = explode('-', $date);
return $exp_date[2] . '-'.$exp_date[1].'-'.$exp_date[0];
}
example: echo changeFormat('2012-12-01'); will return 01-12-2012 and apply this in your while loop where you want to display your date

Categories