Dynamic rowspan in php while loop - php

I need to achieve a result like this through the values in database:
------------------------------------------------------------------------
Question Marks
------------------------------------------------------------------------
Question 1 |
-------------------------------------------------|
Question 2 | 5
-------------------------------------------------|
Question 3 |
but when I do like this:
<?php
while ( $PQRr = mysql_fetch_array($PQRq) ) {
?>
<tr>
<td align="center"><?php echo $PQRr["point_title"]; ?></td>
<td align="center" rowspan="5">
<?php echo $marks_obtained; ?>
</td>
</tr>
<?php } ?>
It actually prints the the last column number of times query executes. How can I make it print just one time?
Here's what I am getting currently.

Try this. Now that part will only be executed once.
<?php
$i=0;
while ( $PQRr = mysql_fetch_array($PQRq) ) {
?>
<tr>
<td align="center"><?php echo $PQRr["point_title"]; ?></td>
<td align="center" rowspan="5">
<?php if(0==$i++) {echo $marks_obtained;} ?>
</td>
</tr>
<?php } ?>

Related

Change only one value of an array returned by sql

I have a payment schedule table where I pull data from the database. The query result fetches four records because there are four payment plans in my table. The code below works fine. The only change I need is here
<td align="left" style="padding-left:5px;">
<?php echo $rr['plan_duration']?>
</td>
I am struggling to put an IF condition for the echo statement I want to see the contents of the array first and then decide what to echo. If the value of $rr['plan_duration'] is 1490 then echo 149 else echo the actual value of $rr['plan_duration'] I am facing issues with mixing html with php as far as the syntax is concerned. Please help me implement this condition. Thanks.
Here is the full working code:
<?php
$result = mysql_query("SELECT * from memship_plan where status='1' order by plan_amount DESC");
while($rr=mysql_fetch_array($result))
{
?>
<tr height="30px">
<td align="left" style="padding-left:5px;" class="red_text">
<?php echo $rr['plan_name']?>
</td>
<td align="left" style="padding-left:5px;">
<?php echo $rr['plan_contacts']?>
</td>
<td align="left" style="padding-left:5px;">
Unlimited
</td>
<td align="left" style="padding-left:5px;">
<?php echo $rr['video']?>
</td>
<td align="left" style="padding-left:5px;">
<?php echo $rr['plan_duration']?>
</td>
<td align="left" style="padding-left:5px;">
Rs.
<?php echo $rr['plan_amount']?>
</td>
<td align="left" style="padding-left:5px;">
<a href="pay.php?plan=<?php echo $rr['plan_name']?>">Pay Now
</a>
</td>
</tr>
PS: I understand the limitation and disadvantages of mysql and I am going to covert it to mysqli
You can insert an entire PHP block inside each td element. Create a function that does the converting from 1490 to 149, let's call it convert() in this example
<td align="left" style="padding-left:5px;">
<?php
if($rr['plan_duration'] == 1490)
{
echo convert($rr['plan_duration'])
}
else
{
echo $rr['plan_duration'];
}
?>
</td>
You can also use the ? conditional to reduce the amount of code:
<td align="left" style="padding-left:5px;">
<?php echo ($rr['plan_duration'] == 1490) ? convert($rr['plan_duration']) : $rr['plan_duration'];
</td>
Note: Besides using mysqli instead of mysql I strongly advice you to use Prepared Statements too
Inside your while loop you can just use an if statement.
<td align="left" style="padding-left:5px;">
<?php if ($rr['plan_duration'] == 1490) {
echo 149 ;
} else {
echo $rr['plan_duration'];
} ?>
</td>
I rewrote your code a bit to make it a bit better to read. I added a shorthand if statement. Take a look:
<?php
$result = mysql_query("SELECT * from memship_plan where status='1' order by plan_amount DESC");
$results = array();
while($record = mysql_fetch_array($result)) {
$results[] = $record;
}
?>
<?php foreach ($results as $rr): ?>
<tr height="30px">
<td align="left" style="padding-left:5px;" class="red_text"><?= $rr['plan_name']; ?></td>
<td align="left" style="padding-left:5px;"><?= $rr['plan_contacts']; ?></td>
<td align="left" style="padding-left:5px;">Unlimited</td>
<td align="left" style="padding-left:5px;"><?= $rr['video']; ?></td>
<td align="left" style="padding-left:5px;"><?= ($rr['plan_duration'] == '1490') ? '149' : $rr['plan_duration']; ?></td>
<td align="left" style="padding-left:5px;">Rs. <?= $rr['plan_amount']; ?></td>
<td align="left" style="padding-left:5px;">Pay Now</td>
</tr>
<?php endforeach; ?>
<?php
$result = mysql_query("SELECT * from memship_plan where status='1' order by plan_amount DESC");
while($rr=mysql_fetch_array($result)) {
?>
<td align="left" style="padding-left:5px;">
<?php if($rr['plan_duration']=='1490') {
echo "149";
} else {
echo $rr['plan_duration'];
}
?>
</td>
<?php } ?>

MySQL query - IF statement to generate two different rows

I have some issues with below script, essentially what I'm trying to achieve is to grab different product and prices and generate a table which works fine. However, some of the products do have an extra charge, in this case the product will use three rows (price, extra charge and total sum). I'm trying to get the IF statement to work as follows: if the extra charge = 0 then it should only make a single row in the table, if more then 0 it should produce the 3 row version.
Someone have any idea what I'm doing wrong? Thanks in advance!
<?php
$page=basename($_SERVER['PHP_SELF']); /* Returns PHP File Name */
$page_name=str_replace(".php","",$page);
mysql_connect(localhost,$dbuser,$dbpass);
#mysql_select_db($database) or die( "Unable to select database");
$query= ("SELECT * FROM table e
JOIN validdate1 r ON e.datevalid1=r.id
JOIN validdate2 d ON e.datevalid2=d.id
WHERE productpage='$page_name'
ORDER BY productname,price2");
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();?>
<table>
<thead>
<tr>
<th class="headerdesc">Description</th>
<th class="headerprice1 rates1">2015</th>
<th class="headerprice2 rates2">2016</th>
</tr>
</thead>
<?php $i=0;while ($i < $num)
{
$productname = mysql_result($result,$i,"productname");
$price1=mysql_result($result,$i,"price1");
$price2=mysql_result($result,$i,"price2");
$extracharge1=mysql_result($result,$i,"extracharge1");
$extracharge2=mysql_result($result,$i,"extracharge2");
$daterange1=mysql_result($result,$i,"daterange1");
$daterange2=mysql_result($result,$i,"daterange2");
if ($extracharge1 > "0") {
echo " <tr>
<td class="desc"><?php echo $productname; ?></td>
<td class="price1 rates1">$<? echo $price1; ?></td>
<td class="price2 rates2">$<? echo $price2; ?></td>
</tr>
<tr>
<td class="extra">Extra Charge**</td>
<td class="price1 rates1">$<? echo $extracharge1; ?></td>
<td class="price2 rates2">$<? echo $extracharge2; ?></td>
</tr>
<tr class="lastrow">
<td class="totalprice"><strong>Total price</strong></td>
<td class="total rates1"><strong>$<? echo $price1+$extracharge1; ?></strong></td>
<td class="total rates2"><strong>$<? echo $price2+$extracharge2; ?></strong></td>
</tr>";
} else {
echo " <tr class="lastrow">
<td class="extra"><?php echo $productname; ?></td>
<td class="price1 rates1">$<? echo $price1; ?></td>
<td class="price2 rates2">$<? echo $price2; ?></td>
</tr>";
}
?>
<?php $i++;}?>
</table>
You have several error in code, change it like this, on the top below query
$num=mysql_numrows($result); will be
$num=mysql_num_rows($result);
don't close the connection since you are still performing queries below
//mysql_close(); comment it out and move it to bottom
and here you need this
if (mysql_num_rows($extracharge1) > 0 )
You are comparing string with a resource in your code
note: Don't use mysql_* functions its deprecated, use PDO or mysqli_*

Force table to display 5 rows with or without data

I have to display a table of 5 rows fetching from MySQL database using PHP. From the code below, if I have 3 rows in database, it will display only 3 rows. But, I need to display 5 rows with 2 empty rows along with 3 fetched rows. If there are no records found in the database, I should display 5 empty rows. I need your help on that.
Note: I am generating a report using PHP and MySQL. From the above method, I can fix the table height and so report will generate without any overlaps.
CODE:
<?php
$select= "select * from table where id=1";
$select2= mysql_query($select);
$select3= mysql_num_rows($select2);
$row_count = 1;
while($row = mysql_fetch_assoc($select2)){
?>
<tr>
<td ><?php echo $row_count;?>.</td>
<td ><?php echo $rows['id']; ?></td>
<td ><?php echo $rows['name']; ?></td>
<td ><?php echo $rows['phone_number']; ?></td>
</tr>
<?php $row_count++;
}?>
As you are already counting the $row_count variable, you can add simple while loop, like this:
<?
while($row_count < 5){
?>
<tr>
<td > </td>
<td > </td>
<td > </td>
<td > </td>
</tr>
<?php $row_count++;
}?>
What is ? Is it needed?
Also check the #paxdiablo's answer about limit 5 option for your query.
This should do the trick.
<?php
$select= "select * from table where id=1";
$select2= mysql_query($select);
$select3= mysql_num_rows($select2);
$row_count = 1;
while($row = mysql_fetch_assoc($select2)){
?>
<tr>
<td ><?php echo $row_count;?>.</td>
<td ><?php echo $rows['id']; ?></td>
<td ><?php echo $rows['name']; ?></td>
<td ><?php echo $rows['phone_number']; ?></td>
</tr>
<?php $row_count++;
}
if ($row_count < 5){
for ($i=1; $i <= (5-$row_count); $i++){
?>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<?php
}
}
?>
You appear to have two distinct problems (albeit related).
If you only want five rows even if your query returns twenty, you can either use the control variable $row_count to only output rows for the first five, or (preferably) just add limit 5 on to your query to get five rows or less.
The second problem is what to do if it returns less than five rows. In that case, use the control variable to output blank rows, something like adding the following to the end:
<?php
while ($row_count < 5) {
?>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<?php
$row_count++;
}
?>
while($row = mysql_fetch_assoc($select2)){
?>
<tr>
<td ><?php echo $row_count;?>.</td>
<td ><?php echo $rows['id']; ?></td>
<td ><?php echo $rows['name']; ?></td>
<td ><?php echo $rows['phone_number']; ?></td>
</tr> <?php }
if($select3<5){
for($i=1;$i<5-$select3;$i++){?>
<tr>
<td ></td>
<td ></td>
<td ></td>
<td ></td>
</tr>
<?php }
}

How to show records from database in html table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm selecting records from database and showing it as
<table>
<tr>
<td align="center" >Name</td>
<td align="center" >Contact</td>
</tr>
<?php
foreach($rowset as $row)
{
?>
<tr>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
</tr>
<?php
}
?>
</table>
It is displayed as
| Name | Contact |
a 9956874525
b 9856426569
c 9865989657
d 8956789565
e 8956879899
f 8985656465
Here each record is shown in separate <tr>
But now i've to show the records in such a way that ,two records are shown in a single <tr>
So my design should be
| Name | Contact | | Name | Contact |
a 9956874525 b 9856426569
c 9865989657 d 8956789565
e 8956879899 f 8985656465
What changes should i do in above code? Please help
You need create an counter to count how many times you have printed the data.
Maybe like this (not tested yet):
<?php
$i = 0;
foreach($rowset as $row)
{
if($i % 2 == 0) echo "<tr>";
?>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
<?php
if($i % 2 == 0) echo "</tr>"; $i++;
}
?>
modify the code like this
<table>
<tr>
<td align="center" >Name</td>
<td align="center" >Contact</td>
</tr>
<?php
$i=1;
foreach($rowset as $row)
{
if($i%2==1) {
?>
<tr>
<?php } ?>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
<?php if($i%2==0) { ?>
</tr>
<?php } ?>
<?php
$i++; }
?>
You can apply the loop conditionally on the basis of indexes.
<table>
<tr>
<td align="center">Name</td>
<td align="center">Contact</td>
<td align="center">Name</td>
<td align="center">Contact</td>
</tr>
<tr>
<?php
foreach($rowset as $i=>$row) {
if($i!=0 && $i%2 == 0) {
?>
</tr><tr>
<?php } ?>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
<?php } ?>
</tr>
</table>
Try using a for loop instead
$num_contacts = sizeof($rowset);
for($i = 0; $i < $num_contacts; $i++)
{
if($i%2 == 0)
echo "<tr>";
?>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
<?php
if($i%2 == 0)
echo "</tr>";
}?>
Its the appropriate way..
<table>
<tr><td></td><td></td><td></td><td></td><td></td></tr>
<tr>
<?php
foreach($rowset as $i=>$row) {
if($i!=0 && $i%2 == 0) {
?>
</tr><tr>
<?php } ?>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
<?php } ?>
</tr>
</table>
First connect to database using: $con = mysql_connect(host,username,password);
Select Database : mysql_select_db(dbname,$con);
Write a query: $qselect = mysql_query("select * from mytable");
Now Let's start displaying in a HTML Table:
<table>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
<?php
while($result = mysql_fetch_array($qselect))
{
extract($result);
?>
<tr>
<td><?= $name?></td>
<td><?= $addr?></td>
</tr>
<?php
}
?>
</table>
Note: $name and $addr are field names from mysql table.

How to hide table if a variable empty

I have table with variables in it. I want to hide whole table if the first variable is empty. I want to hide id="con_industry (whole table) if $list_primary_industry variable is empty. Here is my code:
<table class="admintable" id="con_industry">
<tr id="con_id_industry1">
<td class="key"><?php echo JText::_('PRIMARY_INDUSTRY');
?></td<td>echo $list_primary_industry; ?></td></tr>
</table>
Set a check before your table:
<?php if(isset($list_primary_industry) && !empty($list_primary_industry)): ?>
<table class="admintable" id="con_industry">
<tr id="con_id_industry1">
<td class="key"><?php echo JText::_('PRIMARY_INDUSTRY'); ?></td>
<td>echo $list_primary_industry; ?></td>
</tr>
</table>
<?php endif; ?>
Like this: (fixed your code a bit)
<? if($list_primary_industry) {?>
<table class="admintable" id="con_industry">
<tr id="con_id_industry1">
<td class="key"><?php echo JText::_('PRIMARY_INDUSTRY');
?></td><td><? echo $list_primary_industry; ?></td></tr>
</table>
<?}?>

Categories