Codeigniter explode to html table - php

I'm currently having issue explode to html table.
My Controller
public function idp_test()
{
$this->load->model('groups/groups_model');
$scholar_id = $this->session->userdata('scholar_id');
$groups = $this->groups_model->retrieve_groups_idp($this->scholar->level_id);
$content = array(
'groups' => $groups,
'scholar' => $this->scholar,
'page_title' => 'My individual development plan',
'page_view' => 'scholar/activities/idp_test',
'page_scripts_view' => 'scholar/inc/_choose_activities_scripts',
'sidebar_menu' => 'idp test'
);
$this->load->view("theme/base", $content);
}
My Model
public function retrieve_groups_idp($level_id)
{
$this->db->select("groups.*, levels.name as levelname");
$this->db->join('activities', 'groups.activity_ids = activities.activity_id' );
$this->db->join('levels', 'levels.level_id = groups.level_id', 'left' );
$this->db->join('activity_types', 'activities.activity_type_id = activity_types.activity_type_id', 'left' );
$this->db->where('groups.level_id', $level_id);
$this->db->group_by('groups_name');
$this->db->order_by('groups_name');
$qr = $this->db->get('groups');
return $qr->result();
}
public function retrieve_activity_desc($activity_ids)
{
$activity_desc_arr[] = explode(', ', $activity_ids);
foreach ($activity_desc_arr as $activity_id){
$this->db->select("activity_id as activityid, CONCAT(activities.name, ' - ', activity_types.name) AS description", FALSE);
$this->db->join('activity_types', 'activities.activity_type_id = activity_types.activity_type_id');
$this->db->where_in('activities.activity_id', $activity_id);
$qr = $this->db->get('activities');
return $qr->result();
}
}
My View
<?php foreach ($groups as $groups): ?>
<table class="table table-vcenter">
<thead>
<tr class="active">
<th><?php echo $groups->groups_name?></th> <!--Series Header -->
<th class="text-center"><small><strong>Module Type</strong></small></th>
<th class="text-center"><small><strong>Status</strong></small></th>
<th class="text-center"><small><strong>Action</strong></small></th>
</tr>
</thead>
<tbody>
<tr>
<td><?php
$rows = $this->groups_model->retrieve_activity_desc($groups->activity_ids);
foreach ($rows as $row){
echo var_dump($row->description);
}
?></td>
<td class="text-center">belom lagi</td>
<td class="text-center">TO ATTEND</td>
<td class="text-center">View Session</td>
</tr>
</tbody>
</table>
<?php endforeach ?>
i can't post images, so below are some sample in view so far
SERIES 1 FOR FS | MODULE TYPES | STATUS | ACTION
Series 1 - FILSeries 1 - CBLSeries 1 - PEL | belom lagi | to attend |
and what i need to achieve is like below sample in view
SERIES 1 FOR FS | MODULE TYPES | STATUS | ACTION
Series 1 - FIL | belom lagi | to attend |
Series 1 - CBL | belom lagi | to attend |
Series 1 - PEL | belom lagi | to attend |
I'm newbie with php & codeigniter, sorry with my english.
Thanks

Think about what you have in your view code:
<tr>
<td><?php
foreach($rows as $row){
var_dump($row->description);
}
?>
</td>
<td>belom lagi</td>
<td>to attend</td>
</tr>
Of course this will all be printed into one table cell, how it could produce anything else, right?
What you need is something like this:
<?php
foreach($rows as $row){
echo "<tr>"; // you need a new row for every entry
echo "<td>".$row->description."</td>";
echo "<td>belom lagi</td>";
echo "<td>to attend</td>";
echo "</tr>";
}
?>

After a few studies, i figured it out, just want to share with everybody.
EDITED - Controller
public function idp_test()
{
$this->load->model('groups/groups_model');
$this->load->model('activities/activity_type_model');
$scholar_id = $this->session->userdata('scholar_id');
$groups = $this->groups_model->retrieve_groups_idp($this->scholar->level_id);
foreach ($groups as $group) {
$act_desc = $this->groups_model->retrieve_activity_desc($group->activity_ids);
$group->activity_ids = $act_desc;
}
EDITED - View
<!-- START TABLE HEADER -->
<?php foreach ($groups as $group): ?>
<table class="table table-vcenter">
<thead>
<tr class="active">
<th><?php echo $group->groups_name?></th> <!--Series Header -->
<th class="text-center"><small><strong>Module Type</strong></small></th>
<th class="text-center"><small><strong>Status</strong></small></th>
<th class="text-center"><small><strong>Action</strong></small></th>
</tr>
</thead>
<tbody>
<?php foreach ($group->activity_ids as $session): ?>
<tr>
<td><?php echo $session->description; ?></td>
<td class="text-center"><?php echo $session->modulename; ?></td>
<td class="text-center">STATUS</td>
<td class="text-center">View Session</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<?php endforeach ?>
<!-- END 1 -->
Come out as what i need.
Thank you stackoverflow.

Related

PHP: creating table with conditions

I have array like this in php:
$arr = [
[
"group": 1,
"name": John
],
[
"group": 1,
"name": Luke
],
[
"group": 1,
"name": Peter
],
[
"group": 2,
"name": Pia
]
]
expected output in html table:
GROUP | NAME
------|-----
| John
1 | Luke
| Peter
------|-----
2 | Pia
I attempted to work around the foreach but i cant figure it out. Below is the last code i tried:
<table>
<tr>
<th>GROUP</th>
<th>NAME</th>
</tr>
<tbody>
<?php $group = ''; ?>
<?php foreach($results AS $result) : ?>
<tr>
<td>
<?php if($group !== $result['group']): ?>
<?= $result['group'] ?>
<?php endif; ?>
</td>
<td>
<ul>
<li><?= $result['name'] ?></li>
</ul>
</td>
</tr>
<?php $group = $result['group']; ?>
<?php endforeach; ?>
</tbody>
</table>
but the result is:
GROUP | NAME
------|-----
1 | John
------|-----
| Luke
------|-----
| Peter
------|-----
2 | Pi
I need to show it as the about expected output.
Please help, thanks
You need to make group first like below
<?php
$results = [
["group"=> 1,"name"=> "John"],
["group"=> 1,"name"=> "Luke"],
["group"=> 1,"name"=> "Peter"],
["group"=> 2,"name"=> "Pia"]
];
$newArr = array();
foreach ($results as $key => $value) {
$newArr[$value["group"]][] = $value["name"];
}
?>
<table border="1">
<thead>
<tr><th>GROUP</th><th>NAME</th></tr>
</thead>
<tbody>
<?php foreach($newArr as $group=>$values){ ?>
<tr>
<td><?php echo $group;?></td>
<td>
<ul style="margin: 5px;">
<?php
foreach($values as $key=>$name){
echo "<li>$name</li>";
}
?>
</ul>
</td>
</tr>
<?php } ?>
</tbody>
</table>
Which will give output,
All you need is a little counting and some CSS magic. Add the first block of code anywhere below your array and above your table.
$d = array();
foreach($results as $c){
if(array_key_exists($c['group'],$d)){
$d[$c['group']]++;
}else{
$d[$c['group']] = 1;
}
}
And then change your tbody code to this:
<tbody>
<?php $group = ''; ?>
<?php foreach($results AS $result) : ?>
<?php $t = $result['group']; ?>
<tr>
<?php if($group !== $result['group']): ?>
<?php echo '<td rowspan="' . $d[$t] . '" style="vertical-align:middle;">'; ?>
<?= $result['group'] ?>
</td>
<?php endif; ?>
<td>
<ul>
<li><?= $result['name'] ?></li>
</ul>
</td>
</tr>
<?php $group = $result['group']; ?>
<?php endforeach; ?>
</tbody>
Mind you this is just a mockup that should work but it's very basic and you'll properly need to beautify it a little.
Further more, you could remove the counting if you grouped your array's by "group" instead of having each person hold the group as well.

CodeIgniter : display two query result with one return

I am trying to display 'Category' database and this part is quite easy. The hard part is I need to count the subcategory from the 'Category' database and displaying them with one table.
Category | Sub Category
------------------------
Cat A | 5
Cat B | 7
here is my Model:
function category() {
$query = $this->db->get('category');
$result = $query->result_array();
foreach($query->row() as $q) {
$this->db->where('subcat_id', $q['cat_id']);
$query2 = $this->db->get('subcat');
if($query2) {
return true;
} else {
return false;
}
here is my Controller:
function dispaly_category() {
$data['category'] = $this->mymodel->category();
$this->load->view('view', $data);
}
here is my View:
<table>
<thead>
<th>Category</th>
<th>Subcategory</th>
</thead>
<tbody>
<?php foreach($category as $c) : ?>
<tr>
<td><?php echo $c->category_name; ?></td>
<td><?php echo (count subcat for the above category); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
i just post an answer with the assumption you've one Table only (you don't need a separate table for subcategories, but in case you really need your second table you should be able to derive this based on my answer)
Your Model
function category()
{
$query = $this->db
->select("c.*, count(cc.id) AS countSubCategories")
->from("category c")
->join("category cc", "cc.parent_id = c.cat_id","left")
->group_by("c.id")
->get();
return $query->result();
}
Your Controller
function display_category()
{
$arrViewData = array(
'category' => $this->mymodel->category()
);
$this->load->view('view', $arrViewData);
}
your view
<table>
<thead>
<th>Category</th>
<th>Subcategory</th>
</thead>
<tbody>
<?php foreach($category as $c) : ?>
<tr>
<td><?php echo $c->category_name; ?></td>
<td><?php echo $c->countSubCategories; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

php generated table using rowspan

I am using this code to generate a table which will subsequently be modified to upload data to a mysql database.
<?php
$start_loc_number= 1 ;
$start_loc_alpha= 'A' ;
$end_loc_number= 10 ;
$end_loc_alpha= 'J' ;
$out = '';
$out .= '<table border = 1 bordercolor="#FF0000">';
for($tr='A';$tr<=$end_loc_alpha;$tr++)
{ $out .= "<tr>";
for($td=1;$td<=$end_loc_number;$td++)
{ $out .= '<td BGCOLOR="#99CCFF">'.$tr.$td.'</td>
<td id="sampleID" contenteditable="true"> sampleID</td>
<td id="volume" contenteditable="true"> volume</td>' ;
}
$out .= "</tr>";
}
$out .= "</table>";
echo $out;
?>
At the moment the table generates 3 cells per iteration in 3 column cells,
| coordinates | sample ID | volume |
My question relates to how the php code can be altered so to generate a table where the coordinates cell can be arranged as a rowspan over the sampleID and volume cells positioned in 2 rows with the sample ID over the volume cell
| | sample ID
| coordinates |------------
| | volume
try this,
for($tr='A';$tr<=$end_loc_alpha;$tr++)
{ $out .= "<tr>";
for($td=1;$td<=$end_loc_number;$td++)
{ $out .= '<td BGCOLOR="#99CCFF">'.$tr.$td.'</td><td>
<table><tr style="border-bottom: 1px solid red">
<td id="sampleID" contenteditable="true"> sampleID</td></tr>
<tr> <td id="volume" contenteditable="true"> volume</td></tr></table></td>' ;
}
$out .= "</tr>";
}
This works for me
<table>
<tr>
<td rowspan="2">test</td>
<td>test1</td>
</tr>
<tr>
<td>test2</td>
</tr>
</table>
EDIT : fiddle
you can set rowspan="2" on your coords TD.
You really should not build your output like that.
Try this approach:
<?php
$content = array(
array(
'cords' => 1,
'samp' => "Bar",
'vol' => 13
),
array(
'cords' => 2,
'samp' => "Foo",
'vol' => 456
),
array(
'cords' => 3,
'samp' => "DJ",
'vol' => 34
)
);
?>
<table>
<tbody>
<? foreach($content as $key => $value) { ?>
<tr>
<th rowspan="2">Coordinates: <?= $value['cords'] ?></th>
<td>sample ID: <?= $value['samp'] ?></td>
</tr>
<tr>
<td>volume: <?= $value['vol'] ?></td>
</tr>
<? } ?>
</tbody>
</table>

parse_ini_string show only certain results

I have written some code that parses information from a .ini file into a table.
<?php
$datas = parse_ini_string(file_get_contents( 'https://drive.google.com/uc?export=download&id=pathtoinihere' ), true);
?>
<table border="1" cellspacing="0" cellpadding="5" width="100%">
<tbody class="points-cell">
<tr>
<td class="points-header"><b>Name</b></td>
<td class="points-header"><b>Points</b></td>
</tr>
<?php
foreach( $datas as $section => $data ) {
?>
<tr>
<td class="points-section"><?php echo htmlspecialchars( $section ); ?></td>
<td><?php echo htmlspecialchars( $data["Points"] ); ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
Currently this exports the following:
Name | Points
name1 | 50
unwanted | 7377
name2 | 22
I want to make it so as the .ini file is parsed, the name 'unwanted' and the points related are not parsed.
The way I figure this should be done, and please correct me if I am wrong:
foreach( $datas as $section => $data ) {
if($section=="unwanted"){
$section=="";
}
This is where I get lost. Any guidance helps more than you know.
You can use the continue keyword in PHP for your unwanted items:
foreach( $datas as $section => $data ) {
if($section=="unwanted") continue;
}

How to get several data from a form?

I have two added form. In the first, I choose one client and for him I add several domains names.
Then, when I clik to next, I arrived on a second form in which there are the several domains names (I added before ) in each row with others fields for each one (registar, url and remark). It seems to like this :
toto.com | Directnic | www.toto.com | blablabla
toto.fr | Afnic | www.toto.fr | bla
toto.net | Gandi | www.toto.net | blabla
But when I want to recover this data into my action to add them into my database, I get only the last line. Why ?
My form :
<form action="<?php echo url_for('#add_domaines');?>" method="post">
<?php echo $form->renderGlobalErrors() ?>
<table class="form_add_domaines">
<thead>
<tr>
<th><?php echo $form['nom_domaine']->renderLabel()?></th>
<th><?php echo $form['compte_registar_id']->renderLabel() ?></th>
<th><?php echo $form['url']->renderLabel() ?></th>
<th><?php echo $form['remarque']->renderLabel() ?></th>
</tr>
</thead>
<tbody>
<?php
$domaines = explode("\n",$add_domaines);
for($i=0; $i<count($domaines); $i++)
{?>
<tr>
<td>
<?php
if(1==strcmp(' ', $domaines[$i]))
{
echo "<span style='color:red;'>"."<b>Warning, missing domain name.</b>"."</span>";
}
else
{
echo $domaines[$i];
}
?>
</td>
<td>
<?php echo $form['compte_registar_id']->renderError() ?>
<?php echo $form['compte_registar_id'] ?>
</td>
<td>
<?php echo $form['url']->renderError() ?>
<?php echo $form['url'] ?>
</td>
<td>
<?php echo $form['remarque']->renderError() ?>
<?php echo $form['remarque'] ?>
</td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
<tr>
<td>
<?php echo link_to(__('Back', array(), 'sf_admin'), '#domaine_new') ?>
</td>
<td>
<?php echo $form->renderHiddenFields(false) ?>
<input type="submit" class="submit" value="Add" />
</td>
</tr>
</tfoot>
</table>
</form>
My action :
[...]
$this->form = new AddDomainesForm();
$this->form->setDefault('client_id', $idClient);
$this->form->setDefault('nom_domaine', $noms_domaine);
if($request->isMethod('post'))
{
$name = $this->form->getName();
$values = $request->getParameter($name);
$files = $request->getFiles($name);
$this->form->bind($values, $files);
if($this->form->isValid())
{
print_r($values);break;
//$this->form->save();
$this->getUser()->setFlash('notice', 'OK.');
$this->redirect('#domaine');
}
}
print_r($values) return only the last row of my form
Array ( [compte_registar_id] => 1 [url] => www.toto.net [remarque] => blabla [nom_domaine] => toto.com toto.fr toto.net [client_id] => 17 [_csrf_token] => ... )
Thank you !
When multiple form elements have the same name, only the last one will be taken.
Did you see that cou can send arrays directly?
<inut type="text" name="foobarlist[]">
<inut type="text" name="foobarlist[]">
<inut type="text" name="foobarlist[]">
$_GET['foobarlist'] will return an array with 3 elements.
The brackets are the key.

Categories