I'm trying to learn php and I have this exercise I'm working on. I have an SQL db with two tables: categories and items. I want to loop through categories and create an html table for each category. I can do that part fine. Then for every category, I want to loop through every item and check to see if it belongs to that category. If it does, then it gets passed into the table. However, for some reason the second loop only gets run for the first category.
I've tried using a foreach loop on the items but then nothing shows up.
<?php while($category = mysqli_fetch_assoc($category_result)) : ?>
<button class="collapsible"><?= $category[Name]; ?></button>
<div class="link-data">
<table class="link-table">
<?php while($item = mysqli_fetch_assoc($items_result)) : ?>
<?php if (item[category_id] = category[id] ) : ?>
<tr>
<td><?= $item[name]; ?></td>
<td><?= $item[price]; ?></td>
<td><?= $item[description]; ?></td>
</tr>
<?php endwhile; ?>
<?php endif; ?>
</table>
</div>
<?php endwhile; ?>
I then expect a result like this for example:
Phones :
- Samsung
Toys :
- Rubber ball
Books :
- Harry Potter
Instead I'm getting this:
Phones :
- Samsung
- Rubber ball
- Harry Potter
Toys :
Books :
for the inside loop after first time, item_result pointer goes to end and you have to reset it if you wanna loop again.
so before the inside while add this line
mysqli_data_seek($items_result, 0);
this line set the item_result pointer to first row
Related
I have json html table in php, now i can't get it work that the output in the table will be near each other.
I am already few days busy with this, hope some one can help me.
<?php foreach($data2 as $row): ?>
<tr>
<td><?=$row['model'];?></td>
<td><?=$row['model2'];?></td>
</tr>
<?php endforeach;?>
My json output:
It looks like the JSON is not formatted for what you are trying to do. Every object has either modal or modal2, so the one that is not filled is undefined.
You should get the length of the longest list and use a normal for loop.
<?php ($index = 0; $index <= $lengthOfLongestList; $index++): ?>
<tr>
<td><?=$data2[index]['model'];?></td>
<td><?=$data2[index]['model2'];?></td>
</tr>
<?php endforeach;?>
Im using a custom MVC in PHP
I have two tables.
Controles and professors
professors table contains an id of course --> id_professor
controles table contains a field called --> id_professors in which I store a list of profs ids this way ==> 1;2;5;9
In my datatable Im retrieving a list of controles from controles table
for the Model, Im using this class
public function fetchControlesListe(){
stmt = $this->db->prepare("SELECT * FROM controles ");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
return $stmt->fetchAll();
}
Controller
liste_controles.php
class Liste_Controles extends Controller {
function __construct(){
parent::__construct();
}
$this->view->fetchCtrlListe= $this->model->fetchControlesListe();
$this->view->render('liste_controles/index');
}
VIEW
<table>
<thead>
<tr>
<th>N°</th>
<th>Module</th>
<th>Salle</th>
<th>Surveillants</th>
</tr>
</thead>
<tbody>
<?php foreach($this->fetchCtrlListe AS $key=>$value): ?>
<tr>
<td><?php echo $controle['id_controle']; ?></td>
<td><?php echo $controle['intitule_module']; ?></td> <
<td><?php echo $controle['intitule_salle'];?></td>
<td>
<?php foreach($this->prof as $profs): ?>
HERE, I would like to retrieve the list of profs from professors
table and the field id_profs in professors table stores an
array, as i said before, containing a list of ids (Ex: 1;2;3;4;5)
I would like to loop through that array and retrieve the name of
professor from professors table if the id_professor Exists in that array
. I know I have to create another METHOD but The PROBLEM is i don't know how to pass parameters in the method and call it from the controller to pass it to the view . and the param would be $controle['id_professor']
<?php endforeach; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
The result would be like this:
EDIT:
I know that is not a good idea to store lots of ids as an array, but
my question I just want to know how to do a foreach loop inside
another foreach loop using another method with a paramater that will
be passed from the first loop
There isn't one solution but what you can do is, because you know that id_professors is a string composed of ids separated by commas, you can retrieve an array of identifiers this way :
$ids = explode(",", $profs[id_professors']);
and then
<?php foreach($this->prof as $profs): ?>
<?php $ids = explode(",", $profs['id_professors']); ?>
<?php foreach($ids as $id): ?>
//DO STUFF with $id
<?php endforeach; ?>
<?php endforeach; ?>
Notice that the entire thing is not very beautiful because of opening/closing php tags constantly but I hope you'll make a wonderful templates system for your MVC framework :)
From there, it would not be that nice to access the database directly from the view so a better (not perfect) solution would be to provide the view an array made of professors like this one :
$this->view->profs_list = array(
1 => "Prof 1",
2 => "Prof 2",
//etc.
);
with the key of the array being the profs' identifier.
Then you can retrieve their name through the loop :
<?php foreach($this->prof as $profs): ?>
<?php $ids = explode(",", $profs['id_professors']); ?>
<?php foreach($ids as $id): ?>
<span><?php echo $this->profs_list[$id]; ?></span><br />
<?php endforeach; ?>
<?php endforeach; ?>
I need help with a loop in opencart.
I want to display 2 - 3 product next to each other other on the product page. similar to how products are displayed in on the compare page. I've achieved the product limit via code already and I've managed to display everything else, pictures, price etc. My issue is the attributes.
I know that i need to reach into this array
$this->data['products'][] = array(
......
'attribute' => $attribute_data,
I've also included this line of code
$attribute_groups = $this->model_catalog_product->getProductAttributes($product_id);
my loop is
<?php foreach ($attribute_group['attribute'] as $key => $attribute) { ?>
<tbody>
<tr>
<td><?php echo $attribute['name']; ?></td>
<td><?php echo $attribute['text']; ?></td>
<?php } ?>
<?php } ?>
This gets me all attributes of the current product but displays errors for any attributes not filled in if there are any related products.
Then I have no idea on how to reach into the array and display the related product attribute details.
please help
<?php if isset($attribute['name'];); {?>
<td><?php echo $attribute['name']; ?></td>
<?php } ?>
Try this; the error will not be shown.
This is my first code in php, so my problem might be so obvious. sorry if it is so easy :)
What I'm trying to do is that I am selecting some rows from my data base using
$rrows = Select ( "*" , $tbl_SubForum , null, "p");
$rrows->setFetchMode(PDO::FETCH_CLASS, 'subForum');
I know this works fine.
Each row has the description of a sub forum, containing title and id. I am trying to show sub forum titles in table cells using this code:
<table cellpadding=50px cellspacing=20px BORDER=0>
<?php
$i=0;
while($rrow = $rrows->fetch()){
var_dump($rrow);
?>
<tr>
<td class='subforum' id='subforum1'>
<?php echo $rrow["title"]; ?><br>
Sub forum manager<br>
Posts: 200<br>
Active users: 50<br>
</td>
</tr>
<?php
$i++;
}
?>
the line echo $rrow["title"]; doesn't work and so the page is empty, except for the result of the first var_dump
First var_dump of the first $rrow shows:
as you can see, there actually is a title field in the array and there is only one var_dump so the while loop doesn't work anymore!
why is this happening?
Because $rrow is an object rather than an array, you have to use $rrow->title to access its data member.
What is the best way to make that construction, i have many record with people names, and i need to put them all to 3 strips column, beetween each column huge space, and all names can be arbitrary length, for example
|Bob| |Jonh| |Victor|
|Sergej| |Kennedi| |Mark|
|Antuan| |Kristina| |Cryst|
I need to put them all to that colums in loop automaticaly, and one after the other,
|1| |2| |3|
|4| |5| |6|
|7| |8| |9|
Maybe somebody can advice me how it will be better to do, now i create 3 <ul> and in each ul i have some <li>, but that way will display my record one under the other. And if i use ul i need to get count all record, divide it to 3, and then put in each ul some record, maybe there is better way ?
As #Fabien pointed out, if its tabular data, then you can format it inside a table and use array_chunk() to group them by three's. Consider this example:
<?php
$names = array(
'Bob',
'John',
'Victor',
'Sergej',
'Kennedi',
'Mark',
'Antuan',
'Kristina',
'Cryst',
);
$names = array_chunk($names, 3); // group them by three's
?>
<table border="0" cellpadding="10">
<?php foreach($names as $key => $value): ?>
<tr>
<?php foreach($value as $index => $element): ?>
<td><?php echo $element; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>