Trying to fetch the value from 2 tables in codeigniter code - php

Working on 2 tables (Homework & Deliveries). I am able to fetch the value of "homework_code" from Deliveries Table. Now same field is there in the Homework table as well without primary key.
I am trying to fetch the value of "title" field from a table "homework". both tables has common value of student_id. The code is :
<?php
$invoices = $this->db->get_where('deliveries', array('student_id' => $row['student_id']))->result_array();
foreach($invoices as $row2): ?>
<tr>
<td>
<?php echo $row2['homework_code'];?>
</td>
<td>
<?php echo $this->db->get_where('homework' , array('homework_code'=>'class_id'))->row()->title; ?> <?php echo $row2['title'];?>
</td>
</tr>
<?php endforeach;?>

From the Your Question above, if Your Database Structure is like that then use:-
Homework Table :-
title
student_id
Deliveries Table :-
homework_code
student_id
then Change this:-
<td>
<?php echo $this->db->get_where('homework' , array('homework_code'=>'class_id'))->row()->title; ?> <?php echo $row2['title'];?>
</td>
to this way:-
<td>
<?php
$student_id = $row2['student_id'];
$homework = $this->db->get_where('homework',array('student_id'=>$student_id))->result_array();
echo $homework['title'];
?>
</td>

Based by your code above, you can use Controller like this
public function get()
{
$data = $this->db->get_where('deliveries', array('student_id' => $row['student_id']))->result_array();
$fetch['fetchdata'] = $this->db->get_where('homework', array($data[0]['homework_code'] => 'class_id'))->result_array();
$this->load->view('yourview', $fetch);
}
and get the out put by putting this on View :
<?= $fetchdata[0][title] ?>
However the result_array() function will producing array for the result even it was only one though

Related

php loop problems

noob problem: i have some issues with a loop in php...here is the code (i used the same methodology for other pages and it works); the code it is supposed to display the names of the products from a order, it works, but it is not showing the very first product , i don't know why :
<?php $i=1; while($row_selectOrderItems = mysqli_fetch_array($result_selectOrderItems)){ ?>
<tr>
<td> <?php echo $i; ?> </td>
<td> <?php echo $row_selectOrderItems['pro_name']; ?> </td>
<td> <?php echo $row_selectOrderItems['pro_price']; ?> </td>
<td> <?php echo $row_selectOrderItems['q']; ?> </td>
<td> <?php echo $row_selectOrderItems['q']*$row_selectOrderItems['pro_price']; ?> </td>
</tr>
<?php $i++; } ?>
and here is the code where i used mysqli_fetch_array before the loop
$query_selectOrderItems = "SELECT *,order_items.quantity AS q FROM orders,order_items,products WHERE order_items.order_id='$order_id' AND order_items.pro_id=products.pro_id AND order_items.order_id=orders.order_id";
$result_selectOrderItems = mysqli_query($con,$query_selectOrderItems);
$row_selectOrderItems=mysqli_fetch_array($result_selectOrderItems);
Does anyone have any idea how should i modify this code? Thank you!
You're reading and ignoring the first record in the results. Consider how your loop works:
while($row_selectOrderItems = mysqli_fetch_array($result_selectOrderItems))
Each iteration calls mysqli_fetch_array, stores the record in $row_selectOrderItems, then uses that to display the record. Then consider what you do before the loop:
$row_selectOrderItems = mysqli_fetch_array($result_selectOrderItems);
You're doing exactly that same thing, but not displaying that first record.
Simply remove that first call to mysqli_fetch_array before the loop.
$row_selectOrderItems=mysqli_fetch_array($result_selectOrderItems);
remove this line, so that, it will not read the 1st result at starting.
Now, when you use it in the while loop, it reads the first line
may be you used mysqli_fetch_array($result_selectOrderItems) before this for loop.
check once

Getting an info from the DB with Tables and Updating it

Hi guys im having a difficulty with this scenario:
I want to get the product information using modals, i got this following code:
//Model.php
public function getProduct($product_id){
$this->db->select('product_id,product_name,product_price,product_qty');
$this->db->from('tbl_products');
$this->db->where('product_id',$product_id);
$query = $this->db->get();
return $query->result();
}
//Controller.php
public function view_product(){
$product_id = $this->input->post('product_id');
$this->load->view('header');
$this->data["post"] = $this->Model->ProductList();
$this->load->view('product_page',$this->data);
$this->data["post"] = $this->Model->getProduct($product_id);
$this->load->view('modal/update_product',$this->data);
}
//update_product.php (modal) my View
lets just go straight into the form
<form action="" method="post">
<?php foreach($posts as $post){ ?>
<input type = "hidden" name = "product_id" value = "<?php echo $post->product_id;?>"/>
<input type = "text" name = "product_name" value = "<?php echo $post->product_name;?>"/>
<input type = "text" name = "product_price" value = "<?php echo $post->product_price;?>"/>
<input type = "text" name = "product_qty" value = "<?php echo $post->product_qty;?>"/>
<button type="submit">Update</button>
<?php } ?>
</form>
I got a table already: i can see all products, in my product_page.php
Here is the tables face looks like:
ID Name Price Quantity Option
1 Shoes 150.00 1 Update
2 Liquor 67.50 5 Update
3 Paint 1000.00 5 Update
Once I click the Update button, the update_product.php(a modal) will pop up and get the result of 1 of the product, if I press the first Update only the information for Shoes will be inside the modal, at first i tried it, I get all the information of all the products which makes my modal redundant and looping due to foreach, then I tried getting the information from the table ID itself, and no product pops out, how can I see only 1 product using modal? thank you very much maam and sirs. Please I really need youre help :(
Although I'm not really clear what you're asking, Here is an answer to you question. Your controller code doesn't seem to be making any sense.If you want to display all your products in on page and then you want to edit/update a product when clicked on corresponding update link, here is what you can do.
Use single controller method for list and update
//Controller.php
public function view_product(){
$product_id = (isset($this->input->post('product_id')) ? $this->input->post('product_id'): False ;
if($product_id == False){
$this->data["post"] = $this->Model->ProductList();
$this->load->view('product_page',$this->data);
} else{
$this->data["post"] = $this->Model->getProduct($product_id);
$this->load->view('modal/update_product',$this->data);
}
}
This is simple modification to your code to make it work correctly, but since this code cannot handle form submission of the update form (unless you're pointing update form to a different controller), you will have to add some other code to this controller and your code will get messy in no time. My personal suggestion to you is this,
Use different controllers for list view and update
//Controller.php
public function view_product(){
$this->data["post"] = $this->Model->ProductList();
$this->load->view('product_page',$this->data);
}
public function update_product($product_id){
if($this->input>post('submit')){ // 'submit'should be replaced with the name attribute of your submit button
//call your model and handle the update form submission here
}
$this->data["post"] = $this->Model->getProduct($product_id);
$this->load->view('product_page',$this->data);
}
now, update option of your product_page.php should point to 'update_product' controller method with corresponding product id e.g. {base_url}/controller_class_name/update_product/1
I think your product_page.php has some code like this to loop through all the products and display products in a table, now when you click update link it will point to update_product controller and it will handle the update process.
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Option</th>
</tr>
</thead>
<tbody>
<?php if(!$products){ ?>
<tr>
<td colspan="5">No result</td>
</tr>
<?php } ?>
<?php if($products){ ?>
<?php foreach ($products as $product) { ?>
<tr>
<td><?php echo $product->id; ?></td>
<td><?php echo $product->name; ?></td>
<td><?php echo $product->price; ?></td>
<td><?php echo $product->quantity; ?></td>
<td>
<a href="<?php echo base_url(); ?>controller_class_name/update_product/<?php echo $product->id; ?>" >Update</a>
</td>
</tr>
<?php } } ?>
</tbody>
</table>
You won't be needing a foreach loop inside your update_product.php because you're updating only one product at once.
Hope this is the answer you're looking for, if not please comment and I will edit the answer accordingly.

Multiple values in database field

I have a specifications table for some products. Some products come in different colors. So to make that clear it shows some little color boxes.
So I have table field colors which stores colors like this:
black,white,green
I came this far with the code:
<?php
foreach($result as $kleur) {
$myString = $kleur['kleur'];
$myArray = explode(',', $myString);
print_r($myArray);
?>
<tr>
<td width="20%">Kleur:</td>
<td>
<div class="item">
<img src="http://www.ledframes.nl/images/product_details/accessoires/kleuren/<?php echo $myArray[0];?>.jpg"/></a>
</div>
</td>
</tr>
<?php
}
?>
The print_r shows all the values from the field. And if the change the number at echo $myArray[0] it shows the right color boxes in my specifications table.
Now my question is, if the color field has multiple values how do I loop it so it shows all the color blocks?
I read somewhere that you can use something like this:
foreach( $array as $name => $key)
I have tried to implement this to my code but can't get it to work
If I understand your problem correctly, $result is the array of database records, and for each record (which you call $kleur), $myArray is the array of color names.
So, to show all the color fields, you need another loop over $myArray, e.g. like this:
<?php
foreach($result as $kleur) {
$myString = $kleur['kleur'];
$myArray = explode(',', $myString);
print_r($myArray);
?>
<tr>
<td width="20%">Kleur:</td>
<td>
<?php
foreach($myArray as $colorName) {
?>
<div class="item">
<img src="http://www.ledframes.nl/images/product_details/accessoires/kleuren/<?php echo $colorName;?>.jpg"/></a>
</div>
<?php
}
?>
</td>
</tr>
<?php
}
?>

Show Three records of MySql table in three columns of html table Using PHP

I Need to show three records of MySql table in three different column using php.My Query is
SELECT * FROM TABLE1 WHERE Id IN(1,2,3)
I want to show the result as here
How can i Write LOOP for it?like
while(loop condition)
{
//what will go here?
}
UPDATE: First row fields will show in first column of html table and second record fields will display in second column and so on...I am not asking only show three records
OP saying, it's not so simple. But it is.
So, you have 2 ways to do it.
First. In this case, you are loop through on the 3 columns. Fetch the first row. This put all the data into a div. Class name is column_1. Do it for the other 3. Then floating the divs to left to each other.
$i = 1;
while($row = $db->fetch_row()) {
?>
<div class="column_<?php echo $i; ?>">
<div class="picture">
<?php echo $row["image"]; ?>
</div>
<div class="description">
<?php echo $row["desc"]; ?>
</div>
... and so on...
</div>
<?php
$i++;
}
Second one, when you first collect the data about 3 rows, and then put them into a table rows by row.
<?php
while($row = $db->fetch_row()) {
$results[] = $row;
}
?>
<table>
<tr>
<td><?php echo $result[0]['image'] ?></td>
<td><?php echo $result[1]['image'] ?></td>
<td><?php echo $result[2]['image'] ?></td>
</tr>
<tr>
<td><?php echo $result[0]['desc'] ?></td>
<td><?php echo $result[1]['desc'] ?></td>
<td><?php echo $result[2]['desc'] ?></td>
</tr>
</table>
EDIT
I forgot that, there is a third solution. You can just build the table empty, and then you can update the cells with an ajax call with jQuery.
One way of looping through them is foreach()
assuming you have your results in $results array:
foreach($results as $result) {
//create your table
$result['id']; //has the item id
$result['title']; //has item title
//and so on...
}
HERE is a great tutorial on looping through mysql result sets :D (W3Schools)
Another one HERE
To provide a answer to your comment you must understand how HTML tables work...
<tr> = table row
<td> = table data
You are asking for an entire source code, and this is NOT that place, we don't do your job for you, but if you want, you will have to pay me :) and I am not sure that you agree with this :)
HERE is a good and easy to understand tutorial on HTML tables.
while ($data=mysql_fetch_array($rs)) {
}

displaying table field names dynamically with cake php

I am trying to display a list of all entries in the table people. I have the list of entries generating correctly as a html table but I want to ad a row at the top of the table with the table field names. I could do this manually but figure there must be a way to do it dynamically in case I add or remove fields later on.
here is the controller function
public function peopleDisplay() {
$this->set('people', $this->people->find('all'));
}
Are the field names already in the array that generates? If so how do I reference them? If not how do I get them to be?
Here is the view so far
<table>
<tr>
***were I want the row of field names to go***
</tr>
<?php foreach($people as $people): ?>
<tr>
<td>
<?php echo $people['people']['id'] ?>
</td>
<td>
<?php echo $people['people']['firstName'] ?>
</td>
<td>
<?php echo $people['people']['secondName'] ?>
</td>
</tr>
<?php endforeach; ?>
</table>
The column names are the keys in your array. For instance, 'id', 'firstName', and 'secondName' are the column names.
Another way to approach would be to get the column names as a separate array in your controller and then output them in your view.
$people = $this->People->find('all');
$colNames = array_keys($this->People->getColumnTypes());
$this->set(compact('colNames', 'people'));
Then in your view:
foreach($colNames as $col){ //Output column names }
foreach($people as $person){ //Output people }

Categories