plot mysql data into chart - php

I'm trying to use this chart generator from http://htmldrive.net/items/show/792/Rare-Accessible-charts-using-jQuery-and-HTML5.html
Here's the code which loads data from mysql database:
The query works, but I guess my interpretation of the example provided in the site was wrong.
I get an output if I do it this way(predefined data):
<tr>
<th scope="row">Profit</th>
<td>5</td>
<td>5</td>
</tr>
But when I do it this way I get a blank output:
?>
<table>
<caption> Reports</caption>
<thead>
<tr>
<td></td>
<?php while($row=mysql_fetch_assoc($query)){ ?>
<th scope="col"><?php echo $row['Cust_Name']; ?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Subtotal</th>
<?php while($row=mysql_fetch_assoc($query)){ ?>
<td><?php echo $row['TOTAL_PUR']; ?></td>
<?php } ?>
</tr>
<tr>
<th scope="row">Profit</th>
<?php while($row=mysql_fetch_assoc($query)){ ?>
<td><?php echo $row['TOTALPROFIT']; ?></td>
<?php } ?>
</tr>
</tbody>
</table>
Here's what I'm getting:

After the first iteration through the rows, when you display the customer names, the fetch data pointer is at the end of the dataset... you're trying to fetch the set again without resetting the pointer.
Try issuing
mysql_data_seek($query, 0);
before the while loops to display total and profit

Related

How to pass value in html table tag

In my side project I have a list of song titles and keys that I'm pulling from my database and I'm just wondering how can I use that with an HTML table.
I've been playing around inside the <table> but still no luck so I would be really appreciate if I can get any help or suggestion.
<table>
<tr>
<th>Title</th>
<th>Chord</th>
</tr>
// it will display all the title and chord from here
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Key E</td>
</tr>
</table>
<?php
foreach ($songTitle as $song) {
echo "<a href='details.php?id={$song['id']}'>{$song['title']} {$song['chord']} <br> </a>";
} ?>
First you need to output the table row in the foreach loop, then just use the 2 values in the 2 <td></td> cell definitions
<table>
<tr>
<th>Title</th>
<th>Chord</th>
</tr>
// it will display all the title and chord from here
?php
foreach ($songTitle as $song) :
?>
<tr>
<td><?php echo $song['title'];?></td>
<td><?php echo $song['chord']; ?></td>
</tr>
<?php
endforeach;
?>
</table>
I think this is what are you looking for
<table>
<tr>
<th>Title</th>
<th>Chord</th>
</tr>
<tr>
<td>Example Title</td>
<td>Key E</td>
</tr>
</table>
// display the song title and chord
<?php
foreach ($songTitle as $song) {
<tr>
echo "<td>{$song['title']}</td><td><a href='details.php?id={$song['id']}'>{$song['title']} {$song['chord']} </a></td>";
</tr>
} ?>

how to display comma separated values from multiple columns in php inside html table

i have an sql table with three columns which have comma separated values, i am trying to print it inside an html table, my code looks like below:
<table class="table custom-table m-0">
<thead>
<tr>
<th>Description</th>
<th>Make</th>
<th>UOM</th>
</tr>
</thead>
<tbody>
<?php
$one=explode(',', $row['description']);
$two=explode(',', $row['make']);
$three=explode(',', $row['uom']);
foreach($one as $ones) {
?>
<tr>
<td>
<?php echo $ones?>
</td>
<td></td>
<td></td>
</tr>
<?php }?>
</tbody>
</table>
here am only able to get the values of first column, can anyone please tell me how to get values from all the three columns, thanks in advance
Use a counter - assuming exact same number of entries per row
http://sandbox.onlinephpfunctions.com/code/555be47daf3bc3e99d496585f702bfc9dfae4e4e
<?
$one=explode(',', $row['description']);
$two=explode(',', $row['make']);
$three=explode(',', $row['uom']);
$i=0;
?>
<table class="table custom-table m-0">
<thead>
<tr>
<th>Description</th>
<th>Make</th>
<th>UOM</th>
</tr>
</thead>
<tbody>
<?php
foreach($one as $ones) {
?>
<tr>
<td><?php echo $ones; ?></td>
<td><?php echo $two[$i]?></td>
<td><?php echo $three[$i]?></td>
</tr>
<?php $i++;}?>
</tbody>
</table>

How to combine 'foreach' and value in codeigniter?

I want to view data from database in my website using CodeIgniter 3. There are 2 types of ways to view my data in this website. First is without looping, I used this code :
<?php $s=$sched_stud->row();?>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<th scope="row">Full Name</th>
<td><?php echo $s->name;?></td>
</tr>
<tr>
<th scope="row">Level Enrolled</th>
<td><?php echo $s->level;?></td>
</tr>
</tbody>
</table>
and the second one is using foreach, this is my code :
<tbody>
<?php
$a=1;
foreach ($sched_stud as $key) { ?>
<tr>
<th scope="row"><?php echo $a; $a++; ?>.</th>
<td>Tue, 8 Jan 2019</td>
<td>17.00 - 18.30</td>
<td><?php echo $key->room;?></td>
<td>Mrs. Adinda</td>
<td>Upcoming</td>
</tr>
<?php } ?>
</tbody>
but there are errors saying :
Undefined property: mysqli::$room
how can i fix this?
In first way you are getting only one record with row()
SO in your second way for loop you need result() to get multiple records
<tbody>
<?php
$a=1;
foreach ($sched_stud->result() as $key) { ?>
<tr>
<th scope="row"><?php echo $a; $a++; ?>.</th>
<td>Tue, 8 Jan 2019</td>
<td>17.00 - 18.30</td>
<td><?php echo $key->room;?></td>
<td>Mrs. Adinda</td>
<td>Upcoming</td>
</tr>
<?php } ?>
</tbody>

how to use a variable as a folder name in path [duplicate]

I want to have a link to another page, with particular subject, How can I pass the id ?
<table class="table table-hover">
<thead>
<tr>
<th>Subject</th>
<th>Add Topic</th>
</tr>
</thead>
<tbody>
<tr>
<?php foreach($subjects as $sub):?>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
You still need to echo it out.
<?php foreach($subjects as $sub): ?>
<tr>
<td><?php echo $sub->subject ?></td>
<td>Add Topic</td>
</tr>
<?php endforeach; ?>
Please try the following:
<table class="table table-hover">
<thead>
<tr>
<th>Subject</th>
<th>Add Topic</th>
</tr>
</thead>
<tbody>
<?php foreach($subjects as $sub):?>
<tr>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
and then on the page : approve.php
<?php
$subjectId = $_GET['id'];
?>
$subjectId will give you the corresponding subject id with which you can move forward with the functionality.
Note: foreach should start either outside <tr> and end outside </tr> or it can be inside <tr> </tr>
You need to enclose your variable in a PHP tag:
<?php foreach($subjects as $sub):?>
<tr>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
There is also a short form echo tag enabled on most PHP servers <?= $variable ?>
On the subsequent page you retrieve the parameter from the GET array:
$subject = $_GET['id'];
If you're passing this value to the database you should do some validation:
if ($_GET['id']) { // check parameter was passed
$subject = int_val($_GET['id']) // cast whatever was passed to integer
} else {
// handle no subject case
}
Yes you can, it will be considered as GET. as for how to pass it.
Edit the following:
<td>Add Topic</td>
This is the part:
<?=$sub->id?>
You closed php tag when u started adding html therefore open it again to echo php vars.

PHP passing variable id through href

I want to have a link to another page, with particular subject, How can I pass the id ?
<table class="table table-hover">
<thead>
<tr>
<th>Subject</th>
<th>Add Topic</th>
</tr>
</thead>
<tbody>
<tr>
<?php foreach($subjects as $sub):?>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
You still need to echo it out.
<?php foreach($subjects as $sub): ?>
<tr>
<td><?php echo $sub->subject ?></td>
<td>Add Topic</td>
</tr>
<?php endforeach; ?>
Please try the following:
<table class="table table-hover">
<thead>
<tr>
<th>Subject</th>
<th>Add Topic</th>
</tr>
</thead>
<tbody>
<?php foreach($subjects as $sub):?>
<tr>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
and then on the page : approve.php
<?php
$subjectId = $_GET['id'];
?>
$subjectId will give you the corresponding subject id with which you can move forward with the functionality.
Note: foreach should start either outside <tr> and end outside </tr> or it can be inside <tr> </tr>
You need to enclose your variable in a PHP tag:
<?php foreach($subjects as $sub):?>
<tr>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
There is also a short form echo tag enabled on most PHP servers <?= $variable ?>
On the subsequent page you retrieve the parameter from the GET array:
$subject = $_GET['id'];
If you're passing this value to the database you should do some validation:
if ($_GET['id']) { // check parameter was passed
$subject = int_val($_GET['id']) // cast whatever was passed to integer
} else {
// handle no subject case
}
Yes you can, it will be considered as GET. as for how to pass it.
Edit the following:
<td>Add Topic</td>
This is the part:
<?=$sub->id?>
You closed php tag when u started adding html therefore open it again to echo php vars.

Categories