Convert Sql table field input into HTML table - php

my Database table field look like this
id - name - school - subjects - marks
1 - Adam - Highschool - Geography,physics,math - 60,50,40
2- - Jhone - elementry - Math,Language,Geography - 90,20,10
the php file should convert this values into html table using boot strap
my expected reslut to be like this
Hello Jone! this is your results
Math : 90
Language : 20
Geography : 10
I tried different methods like convert the result into array but it did work with me will.
one of them are
<?php
$shop = array( array("title"=>"rose", "price"=>1.25 , "number"=>15),
array("title"=>"daisy", "price"=>0.75 , "number"=>25),
array("title"=>"orchid", "price"=>1.15 , "number"=>7)
);
?>
<?php if (count($shop) > 0): ?>
<table>
<thead>
<tr>
<th><?php echo implode('</th><th>', array_keys(current($shop))); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($shop as $row): array_map('htmlentities', $row); ?>
<tr>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
any advice
thanks

It will be great to normalize your data and to avoid any comma separated lists in your database. However you can achieve this very easily using explode():
Input:
mysql> select * from marks;
+----+------+-------------+-------------------------+----------+
| id | name | school | subjects | marks |
+----+------+-------------+-------------------------+----------+
| 1 | Adam | High School | Geography,physics,math | 60,50,40 |
| 2 | Jone | Elementary | Math,Language,Geography | 90,20,10 |
+----+------+-------------+-------------------------+----------+
2 rows in set
PHP code:
<?php
$con = mysqli_connect('localhost','root','','test') or die(mysqli_error($con));
$query = "SELECT * FROM marks";
$result = mysqli_query($con,$query) or die(mysqli_error($con));
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
$subject = explode(',',$row['subjects']);
$marks = explode(',',$row['marks']);
echo 'Hello '.$row['name'].'! This is your results:';
echo '<table>';
foreach($subject as $k=>$s){
echo '<tr>
<td>'.$s.'</td>
<td>'.$marks[$k].'</td>
</tr>';
}
echo '</table>';
}
}
?>
Output is table for each user:
Hello Adam! This is your results:
Geography 60
physics 50
math 40
Hello Jone! This is your results:
Math 90
Language 20
Geography 10

Related

MySQL Data in the PHP Array

I have a table as follows that is used to store details of employees:
+---------+---------+------------------+
| of_code | name | employment_status |
+---------+---------+------------------+
| 1 | Jhon | 1 |
| 2 | Ram | 1 |
| 3 | Edward | 3 |
| 4 | William | 2 |
+---------+---------+------------------+
This is one of the options in my Codeigniter project.
Desired output
I want to show the employment_status in separate columns in the view as per the value in it. If employment_status = 1, it should be in the first column. If employment_status = 2, it should be in the second column. If employment_status = 3, it should be in the the column and so on.
Controller (Relevant Part)
$where = NULL;
$this->data['officerList'] = $this->Officer_model->officerSummary($where);
Model
function officerSummary($where){
$q = $this->db->query("SELECT
of_code, name, employment_status as cnt
from tbl_officer $where
");
if ($q->num_rows() > 0) {
return $q->result();
}
}
View (Relevant Part)
<?php
$officerLists = [];
foreach ($officerList as $row) {
if (!in_array($row->cnt, $officerLists)) {
$officerLists[] = $row->cnt;
}
}
?>
<div class="table-responsive" id="datatable">
<table id="ExData" cellpadding="0" cellspacing="0" border="0"
class="table table-bordered table-condensed table-hover table-striped reports-table">
<thead id="th">
<tr class="" style="background-color: #d966ff !important;">
<th>#</th>
<th style width="5%" class="text-center"><font size="1"> Code</th>
<th style width="15%" class="text-left"><font size="1">Name</th>
<th class="text-center" colspan="<?= count($officerLists) ?>"><font size="1">Employment Status</th>
</tr>
</thead>
<tbody>
<?php
$count = [];
if(!empty($officerList)) {
foreach ($officerList as $rows) {
$total += $rows->cnt;
$c++;
?>
<tr>
<td><font size="1"><?= $rows->of_code ?></td>
<td><?= $rows->name ?></td>
<?php
foreach ($officerLists as $value) {
if ($rows->cnt == $value) {
if (isset($count[$value]))
$count[$value] = $count[$value] + $rows->of_code;
else
$count[$value] = $rows->of_code;
echo "<td class='text-center'></td>";
} else
echo "<td class='text-center'>-</td>";
}
}
?>
</tbody>
</table>
The function outs three columns correctly, but with no values. Only '-' shows non-relevant places in the column as follows.
What may be going wrong? Can anyone help?
Maybe using a SELECT - CASE structure can be your option
A query in this way
SELECT of_code,
name,
CASE WHEN employment_status = 1 THEN employment_status AS StatusA,
CASE WHEN employment_status = 2 THEN employment_status AS StatusB,
CASE WHEN employment_status = 3 THEN employment_status AS StatusC
FROM yourTbale;
Every CASE will verify if the status is 1 or 2 or 3
Th previous step will generate a new column with a given alias
Because we need the status value as cell value in our result I put it again after the THEN clause
If this work, you only will need to adapt it in CI sintax

Saving 'if conditon statement' in database and to use it in php form

I have a table named user where I have inserted a value like
($workshop_sales*0.01)+($counter_sales*0.005)+($sub_dealer_sales*0.0075)
in column incentive and value like
if{$workshop_sales>1600000' in column 'open_cond' and value like '}
in close_cond.
Now in my PHP page I have fetched values from that table. How can I use values from my columns open_cond and close_cond because I wish to use it as expression otherwise please help me by giving other way to execute it.I am doing wrong then please give me information whether its possible that I can write 'if statement' in php using it store as a text value in database column value ?? ''
I am getting result as
if($workshop_sales>1600000){2123.085}
but I want to use it as PHP expression in my code.
<?php
$selectalldepartment = "select * from user ";
$selectedalldepartment = mysqli_query($conn,$selectalldepartment);
$selectedalldepartment = mysqli_query($conn,$selectalldepartment);
$depart=1;
while($row10 = mysqli_fetch_assoc($selectedalldepartment))
{
$sqlsel1 = "select * from parameters ";
$sqlsel1q = mysqli_query($conn,$sqlsel1);
$row2 = mysqli_fetch_assoc($sqlsel1q);
//assigned counter sales value
$counter_sales = $row2["counter_sales"];
?>
<tr>
<td style="text-align:center;"><?php echo $depart; ?></td>
<td style="text-align:center;"><?php echo $row10["open_condition"]; eval("echo {$row10['incentive']};");echo $row10["close_condition"];?></td> </tr>
<?php
$depaz++;
$depart++;
}
?>
Here is my table:
<!-- language: lang-none -->
user_id | user_name | incentive |open_cond | close_cond |
1 | Goldy Shahu | ($workshop_sales*0.01)+ | |
| | ($counter_sales*0.005)+ | |
| | ($sub_dealer_sales*0.0075)|if($workshop_sales> |
1600000){ |}
you should not store conditions into your table
need to store data like
$workshop_sales and 1600000 in seporate columns
then you can make condition
like something
if($row10["open_condition"] > $row10["open_condition_value"]){
//your code
}

Create table column with the number of a particular column with php

I have an array of data and want to display it on a table, but the columns that appear in the table according to the number of array data, how to make 12 column array default but the number remains on the database. suppose there are 2 data appear in the table but there are a number of columns 12...?
Models
function get_id($id) {
$this->db->where('my_id', $id);
$get_data = $this->db->get('mytable');
if ($get_data->num_rows() > 0) {
foreach ($get_data->result() as $data) {
$my_result[] = $data;
}
return $my_result;
}
}
Controllers
public function myControllers() {
$id = $this->uri->segment(4);
$data=array('detail' => $this->mymodels->get_id($id));
$this->load->view('layout/wrapper', $data);
}
Views
<table>
<thead>
<tr>
<td>No</td>
<td>ID</td>
<td>Name</td>
<td>Class</td>
</tr>
</thead>
<tbody>
<?php
$no = 1;
foreach ($detail as $row) {
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td>'.$row->id.'</td>';
echo '<td>'.$row->name.'</td>';
echo '<td>'.$row->class.'</td>';
echo '</tr>';
$no++;
}
?>
</tbody>
</table>
I expected as this example
NO | ID | NAME | ClASS |
____|____ |______|_______|
1 | 001 | Paul | x |
2 | | | |
3 | | | |
4 | | | |
to 12
If I'm understanding your question, you want to have a default of 12 rows in your table, even if only 2 rows are filled with data.
What about adding a while loop after your foreach loop that continues incrementing your counter and adding rows until your counter reaches 12?
while ($no <= 12) {
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo '</tr>';
$no++;
}

Split ID's Up In Tables In While Loop

I have the following database table:
AWARD_ID | NOMINEE_ID | VOTER_ID | MULTI_CODE
------------------------------------------------------
5 | 3 | 1 | 9326
5 | 4 | 1 | 9326
5 | 5 | 3 | 8746
I need to display these results in tables grouped by MULTI_CODE, so for example:
So it would look like
<h1>Multi Code: 9326</h1>
<table>
<tr>
<td>Nominee: 3</td><td>Nominee: 4</td>
</tr>
</table>
<h1>Multi Code: 8746</h1>
<table>
<tr>
<td>Nominee: 5</td>
</tr>
</table>
Here is my SQL + PHP so far:
$nomineedetails = mysqli_query($con,"SELECT AWARD_ID, NOMINEE_ID, VOTER_ID, MULTI_CODE
FROM b_awards_votes WHERE AWARD_ID = '5'");
$multi_code = -1;
while($row = mysqli_fetch_array($nomineedetails))
{
$awardID = $row['AWARD_ID'];
$nomineeID = $row['NOMINEE_ID'];
$voterID = $row['VOTER_ID'];
$multiCode = $row['MULTI_CODE'];
print "<h2>$multiCode</h2>";
if ($multi_code != $multiCode) {
print "<table><tr>";
$multi_code = $multiCode;
}
print "<td>Nominee: $nomineeID</td>";";
}
</tr></table>
With this I get this:
8746
9326
Nominee: 3
9326
Nominee: 4 Nominee: 5
Why am I getting the 9326 above the Nominee 3?
First of all, I suggest to order your results by MULTI_CODE to have the correct sorting when looping through the results.
Then, in your loop you always print the headline with the multi code, regardless being different to the previous one.
Please have a look at this code. It isn't tested, so please be aware that there might be errors in it:
$nomineedetails = mysqli_query($con,"SELECT AWARD_ID, NOMINEE_ID, VOTER_ID, MULTI_CODE
FROM b_awards_votes WHERE AWARD_ID = '5' ORDER BY MULTI_CODE ASC");
$multi_code = -1;
while($row = mysqli_fetch_array($nomineedetails))
{
$awardID = $row['AWARD_ID'];
$nomineeID = $row['NOMINEE_ID'];
$voterID = $row['VOTER_ID'];
$multiCode = $row['MULTI_CODE'];
if ($multi_code != $multiCode) {
if($multi_code !== -1) {
print "</table>";
}
print "<h2>$multiCode</h2>";
print "<table>";
$multi_code = $multiCode;
}
print "<tr><td>Nominee: $nomineeID</td></tr>";
}

how to replace value with content from database?

i have this on my database table
id_a | nama | email |
1 | philipe | philipe#mail.com |
2 | dora | dora#mail.com |
3 | John | john#mail.com |
this code on my view
<tr>
<td><label for="cities">Select attendances</label></td>
<td> : </td>
<td> <select class="multiselect" multiple="multiple" name="id_c[]" title="click to select Attendances">
<?php foreach ($test as $data): ?>
<option value="<?php echo $data->id_a ?>" ><?php echo $data->nama ?></option>
<?php endforeach; ?>
</select></td></tr>
in my code i have option value from my database = 1 2 3
what i want to do is send email, with an email address from my database
$id_c = $this->input->post('id_c');
foreach($id_c as $id_a)
{
$query="SELECT email From mstr_attend WHERE id_a = ".$id_a.""; }
and here's the problem
what i get from this code is philipe#mail.comdora#mail.comjohn#mail.com
what i need is philipe#mail.com, dora#mail.com, john#mail.com
a comma after one email
can someone,give me some solution so i can get that
Assuming you want a string, you could loop into an array and then implode.
$emails = array();
foreach( $ids as $i )
{
//do query here, then store in the emails array
$emails[] = $query_output_email;
}
$comma_separated = implode( ', ', $emails ); //should output as email#dot.com, email#dot.com
You can get the stated result using the following code:
$id_c = $this->input->post('id_c');
$all_id_c = implode(',', $id_c);
$this->db->select('group_concat(email) as emails');
$this->db->where('id_a IN ('.$all_id_c.')');
$query = $this->db->get('mstr_attend');
$result = $query->row_array();
echo $result['emails']; // here is your result.
This link will surely help you to generate the query result.

Categories