I want to put text file to table in php - php

I have a text file:
[{"title": "...", "Book": ["A", "B"]}]
How can I read it and put into table using php??

Do something like this.
if you need to read a text file then use file_get_contents() function.
<?php
$fh = file_get_contents('text.txt');
$table = json_decode($fh);
?>
<table>
<thead>
<tr>
<th>Head 1</th>
<th>Head 2</th>
</tr>
</thead>
<tbody>
<?php foreach($table as $val){ ?>
<tr>
<td><?php echo $val->title; ?></td>
<td><?php echo $val->Book[0]; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
NOTE Set your table head as per your need.

Related

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 HTML table retrieve multiple data in one <td> tag

I'm trying to generate a list from database in a HTML table just like image below;
https://i.stack.imgur.com/61XLl.png
And here's what i did;
https://i.stack.imgur.com/lLsvF.png
And the code;
<table cellpadding="3" border="1" style="width:100%;margin-top:30px; margin-bottom:50px; font-size:12px">
<thead>
<tr>
<th>KURSUS</th>
<th rowspan="2">NAMA PENSYARAH</th>
<th rowspan="2">NO. SIRI</th>
</tr>
<tr>
<th>NAMA</th>
</tr>
</thead>
<tbody align="center">
<?php
if($numrow>0)
{
while($row = $select->fetch_assoc()){
$code=explode("/",$row['po_code']);
$list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC");
?>
<tr>
<td><?php echo $row['po_name']; ?></td>
<?php while($rowlist = $list->fetch_assoc()){
$name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'");
$rowname=$name->fetch_array();?>
<td><?php echo $rowname['user_name']; ?></td>
<td><?php echo $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']; ?></td>
<?php } ?>
</tr>
<?php
}
}
?>
</tbody>
</table>
Help me. Thank you in advance :)
Use this code. Concat user names and code with "br" tags in the second while loop and display them in "tds" after while loop.
<tbody align="center">
<?php
if($numrow>0)
{
while($row = $select->fetch_assoc()){
$code=explode("/",$row['po_code']);
$list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC");
?>
<tr>
<td><?php echo $row['po_name']; ?></td>
<?php
$user_names = $codes = ''; // define empty variables
while($rowlist = $list->fetch_assoc()){
$name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'");
$rowname=$name->fetch_array();
$user_names .= $rowname['user_name']."<br/>"; //concat to a single string
$codes .= $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']."<br/>"; //concat to a single string
}?>
<td><?php echo $user_names;?></td>
<td><?php echo $codes;?></td>
</tr>
<?php
}
}
?>
</tbody>
Put the <td> outside the <?php while($rowlist = $list->fetch_assoc()){
Or get all your data before you start display html and store it in a multi-dimensional array. Then simply loop through the data array. That way you won't have as much php mixed with html also.

How to set different styles for different rows in html table created from php array?

I'm struggling with creating a html table from php array, while applying different css classes on different rows according to the array.
I have this code in data.php:
<?php
$data = array(
array('Title 1'=>'text11', 'Title 2'=>'text12'),
array('Title 1'=>'text21', 'Title 2'=>'text22'),
array('Title 1'=>'text31', 'Title 2'=>'text32'),
array('Title 1'=>'text41', 'Title 2'=>'text42', 'special'=>'style1'),
array('Title 1'=>'text51', 'Title 2'=>'text52', 'special'=>'style2'),
);
?>
I want to create a html table from this array, and if the array contains 'special'=>'style', it would set that style to that particular row. This is my code so far:
<?php include('data.php'); ?>
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key=>$row):
if ($row == 'class1') {
$class='class="style1"';
} elseif ($row == 'class1') {
$class='class="style2"';
} else {
$class='';
}?>
<tr <?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
And this is the desired output:
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>text11</td><td>text12</td>
</tr>
<tr>
<td>text21</td><td>text22</td>
</tr>
<tr>
<td>text31</td><td>text32</td>
</tr>
<tr class="style1">
<td>text41</td><td>text42</td>
</tr>
<tr class="style2">
<td>text51</td><td>text52</td>
</tr>
</tbody>
</table>
Your problem is this line:
<?php foreach ($data as $key=>$row):
You're forgetting that you're looping over a multidimensional array (i.e. an array of arrays) so that $row is an array here. On the next line:
if ($row == 'class1')
You're looking for a comparison between a string and $row which is an array. This will never work! As Daniel points out in his answer, you need to look at the contents of the array.
Don't forget you'll need to remove the special element from the array before displaying it. Personally, I'd condense the code a bit, though mixing PHP and HTML like this is never a good idea.
<?php include('data.php'); ?>
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $row): $class = $row["special"] ?? ""; unset($row["special"]);?>
<tr class="<?=$class?>">
<td><?=implode("</td><td>", $row)?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I'm not sure I understood everything but you can try this
<?php foreach ($data as $key => $row):
$class = '';
if(isset($row['special'])){
$class = 'class="'.$row['special'].'"';
unset($row['special']);
}
?>
<tr <?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
If I understand your question correctly, this should do the job.
The result is as you desire, see: Online PHP shell
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key=>$row):
if (isset($row["special"])) {
$class = " class='" . $row["special"]. "'";
unset($row["special"]);
} else {
$class='';
}?>
<tr<?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

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.

plot mysql data into chart

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

Categories