Foreach not giving me any result - php

Hello guys!
I'm trying to use a foreach cicle to show some images from my disk but something is getting wrong. It show and error saying
Message: Array to string conversion
Line Number: 306
Here is my foreach cicle
<div class="col-lg-9">
<div class="row">
{foreach $products as $product}
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img class="card-img-top border-bottom" src="{$product.image}" alt="">
<div class="card-body">
<h6 class="card-title">
<p class="text-dark">{$product.name}</p>
</h6>
</div>
<button type="button" class="btn btn-secondary"><i class="material-icons">local_grocery_store</i><span class="float-right mr-4">Adicionar ao carrinho</span></button>
</div>
</div>
{/foreach}
</div>
</div>
My error said the problem is on this line
$data['products'] = '.base_url(' . $this->Cart_model->get_img() . ').';
Also my model with get_img() is this
public function get_img(){
$sql = "SELECT * FROM products";
$query = $this->db->query($sql);
return $query->result();
}
and this is my database
Data base example

I think the issue is that you're using base_url to call the model, remove the base_url and then call the model. Like this -
$data['products'] = $this->Cart_model->get_img(); // returns array of objects
In your view file, use foreach to traverse the data then you'll have to do something like
<?php echo base_url()."path/to/folder/$product->table_image_column"; ?>
View (in php) // this is how I'd do.
<?php foreach($products as $product) { ?>
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img class="card-img-top border-bottom" src="<?php echo base_url()."your/path/$product->image"; ?>" alt="">
<div class="card-body">
<h6 class="card-title">
<p class="text-dark"><?php echo $product->name; ?></p>
</h6>
</div>
<button type="button" class="btn btn-secondary"><i class="material-icons">local_grocery_store</i><span class="float-right mr-4">Adicionar ao carrinho</span></button>
</div>
</div>
<?php } ?>
or if you're using any framework and don't want to mingle php in it. Use foreach in the controller itself and add base_url() to the image. You don't have to make any changes in the view for this-
Controller
$i= 0;
foreach($products as $product) {
$products[$i]->image = base_url()."path/to/image/$product->image"; // change the products array with new values
$i++;
}
See if it helps you.

The answer is add src="<?php echo base_url('{$product.image}'); ?>" in the Views
<div class="row">
{foreach $products as $product}
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img class="card-img-top border-bottom" src="<?php echo base_url('{$product.image}'); ?>" alt="" style="width:30px;height:30px;">
<div class="card-body">
<h6 class="card-title">
<p class="text-dark">{$product.name}</p>
</h6>
<div class="text-danger"><b><span>{$product.price}€ </span></b><span class="text-secondary float-right"><del>{number_format((float)$product.price*1.5, 2, '.', '')}€</del></span></div>
</div>
<button type="button" class="btn btn-secondary"><i class="material-icons">local_grocery_store</i><span class="float-right mr-4">Adicionar ao carrinho</span></button>
</div>
</div>
{/foreach}
</div>

Related

How to access form element in while loop in PHP

i want to get acces to comment and get the value of it , i used $_POST but i get error
Undefined array key
the code create photo posts with while loop the post also contains the comment section.
here the Photo post code:
<!-- Photos section -->
<section>
<div class="container mt-5">
<div class="album py-5 bg-light">
<div class="row row-cols-1 row-cols-sm-1 row-cols-md-1 row-cols-lg-1">
<div class="container">
<?php
if (mysqli_num_rows($res_for_pics) > 0) {
while ($pics = mysqli_fetch_assoc($res_for_pics)) { ?>
<div class="col mb-3">
<div class="card shadow-sm">
<img src="images/user/<?= $pics['pic'] ?>" class="card-img-top img-fluid" alt="...">
<div class="card-body">
<h5 class="card-title" id="add-com"><?php echo $pics['pic_title'] ?></h5>
<p class="card-text"><?php echo $pics['pic_des'] ?></p>
<div class="d-flex justify-content-between align-items-center">
<small class="text-muted mb-3">by nithan</small>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="headingOne">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
<h5 class="card-title"> Comments</h5>
</button>
</h2>
<div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
<div class="accordion-body">
<ul class="list-group list-group-flush">
<li class="list-group-item">
howa
<p class="card-text"><small class="text-muted"></small></p>
</li>
</ul>
</div>
</div>
</div>
**I Want to get access to the value of textarea in the other php file**.
<form method="$_POST" action="../model/comment.php">
<div class="mb-3 mt-5">
<h5 for="message-text" class="col-form-label">add your comment</h5>
<textarea type="text" class="form-control" id="message-text" name="theComment"></textarea>
</div>
<button type="submit" class="btn btn-primary text-light" name="addComment">Comment</button>
</form>
</div>
</div>
</div>
<?php }
} ?>
</div>
</div>
</div>
</div>
</section>
and here comment.php
<?php
include('db_con.php');
$comment = $_POST["theComment"];
echo $comment;
So i try to get access to the 'theComment' value from the form .

updating images with multi image form

I want to be able to update (replace and delete the image)
What I am trying to do is get the Id from the database by attaching it to the name array of the form. I cannot get the record ID from the database.
Form
<div class="container-fluid">
<form action="Multi_imgs/update_image" method="POST" enctype="multipart/form-data">
<div class="row row-sm">
<?php foreach ($model as $item) : ?>
<div class="col-md-3">
<div class="card mb-2" style="width: 18rem;">
<img src="<?= $item->image_name ?>" class="card-img-top" alt="..." style="object-fit:scale-down;height:150px">
<div class="card-body">
<h5 class="card-title">
Delete
</h5>
<p class="card-text">
<div class="form-group">
<label for="" class="form-control-label">
<span class="text-danger">
*
</span>
Change Image
</label>
<div class="">
</div>
<input type="file" name=<?= "multi_img[$item->id]" ?> id="">
</div>
</p>
</div>
</div>
</div>
<?php endforeach ?>
</div>
<div class="col-md-12 col-sm-12 d-flex justify-content-end">
<input class="btn btn-primary btn-md-lg btn-sm-sm" type="submit" value="Update Images">
</div>
</form>
</div>
Controller
public function update_image()
{
$model = new MultiImagesModel();
if ($this->request->getMethod('POST')) :
$images = $this->request->getFileMultiple('multi_img');
foreach ($images as $id => $image) :
$model = new MultiImagesModel();
$result = $model->find($id);
//unlink($result->image_name);
//print_r($image);
echo '<pre>';
$result;
echo '</pre>';
endforeach;
endif;
}

<a href=""> does not go to another page when pass value

I have a display_all_groups.php page that display a bootstrap card of all groups created by user. When users click on "View Group" hyperlink it will direct them to view_group.php
Here are my code
session_start();
require_once $_SERVER["DOCUMENT_ROOT"].'/mindspace/Business Services Layer/ManageGroupsController/GroupsController.php';
$std_id = $_SESSION['std_id'];
$group = new ManageGroupsController();
$data = $group->viewGroups($std_id);
?>
<div class="main-panel">
<div class="content-wrapper">
<div class="row">
<div class="col-12 grid-margin stretch-card">
<form method="post">
<?php
foreach($data as $row){
?>
<div class="card-body">
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="../../template/images/banner.png" alt="Card image cap">
<div class="card-body">
<h5 class="card-title"><?=$row['group_name']?></h5>
<p class="card-text"></p>
<a class="btn btn-primary" href="view_group.php?group_id=<?=$row['group_id']?>">View Group</a>
</div>
</div>
</div>
</form>
<?php } ?>
</div>
</div>
</div>
</div>
When I try to click on the View Group button, it did not bring me to other page that I want it to. Please help, Im very new to PHP
It's hard to say exactly what the problem could be. First I would always check that $data is not empty. Then the keys are named correctly. I don't know the data object.
If everything is correct, then it should work. A small note: In the link there is still a simple quotation mark. You should delete that too.
<?= $row['group_id']?> >>>>**'**<<<< "
<?php session_start();
require_once $_SERVER["DOCUMENT_ROOT"] . '/mindspace/Business Services Layer/ManageGroupsController/GroupsController.php';
$std_id = $_SESSION['std_id'];
$group = new ManageGroupsController();
$data = $group->viewGroups($std_id);
?>
<div class="main-panel">
<div class="content-wrapper">
<div class="row">
<div class="col-12 grid-margin stretch-card">
<form method="post">
<?php
foreach ($data as $row) {
?>
<div class="card-body">
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="../../template/images/banner.png" alt="Card image cap">
<div class="card-body">
<h5 class="card-title"><? echo $row['group_name']; ?></h5> <!-- Used echo -->
<p class="card-text"></p>
<a class="btn btn-primary" href="view_group.php?group_id=<? echo $row['group_id']; ?>">View Group</a> <!-- used echo, removed extra single quote -->
</div>
</div>
</div>
</form>
<?php } ?>
</div>
</div>
</div>
</div>
I had done a few changes in the code,
Used echo
Removed an additional single quote

print a value once time in foreach

I'm new into PHP. I googled this question but cannot get value still.
I have a PHP project where
<?php
foreach($categories as $key => $cat){
?>
<div class="container-fluid ">
<div class="row justify-content-around mt-5">
<div class="card border-0" style="width: 25rem;">
<div class="display-3"><?php echo $cat ?></div>
<img style="margin-bot: 100px;" class="card-img-top" src="imagenes/drinks3.png" alt="Card image cap">
<div class="card-body">
</div>
</div>
<div class="card border-0" style="width: 25rem;">
<div class="display-3"><?php echo $cat ?></div>
<img class="card-img-top" src="imagenes/snacks.png" alt="Card image cap">
<div class="card-body">
</div>
</div>
<div class="card border-0" style="width: 25rem;">
<div class="display-3"><?php echo $cat ?></div>
<img class="card-img-top" src="imagenes/postres1.png" alt="Card image cap">
<div class="card-body">
</div>
</div>
</div>
</div>
I need to print each value once time to get a card with each category of $categories.
But im getting 9 cards with each category repeated 3 times.
I tried with index but it only get broken with offset 0.
Print the nested div you need to be unique in a loop:
<div class="container-fluid ">
<div class="row justify-content-around mt-5">
<?php
foreach($categories as $key => $cat){
?>
<div class="card border-0" style="width: 25rem;">
<div class="display-3"><?php echo $cat ?></div>
<img style="margin-bot: 100px;" class="card-img-top" src="imagenes/drinks3.png" alt="Card image cap">
<div class="card-body">
</div>
</div>
<?php } ?>
</div>
</div>

Single php while one three different classes of images

i am using a main fours divs first div has class ad1, second and third div has class ad2 and then last div has class ad3...
i want to display 4 images from database
How to do it through while loop, please help, Thanks :)
here is html code and the php code....
<div class="col-md-9 hidden-sm hidden-xs">
<div class="col-md-1"></div>
<div class="col-md-2">
<div class="ad1">
<img src="" alt="">
</div>
</div>
<div class="col-md-2">
<div class="ad2">
<img src="" alt="">
</div>
</div>
<div class="col-md-2">
<div class="ad2">
<img src="" alt="">
</div>
</div>
<div class="col-md-5">
<div class="ad3">
<img src="" alt="">
</div>
</div>
</div>
php code is
<?php
$query = "SELECT * FROM headerimages limit 4";
$result = mysqli_query($con, $query) or die(mysqli_error($query));
while ($row = mysqli_fetch_array($result))
{?>
<?php
}
?>
First of all I recommend to use pdo database driver.
In your code the fast fix will be to assign result to array and simply echo it in the view.
$result = array();
while ($row = mysqli_fetch_array($result))
{
$result[]=$row['img'];
}
and in view echo it
<div class="col-md-9 hidden-sm hidden-xs">
<div class="col-md-1"></div>
<div class="col-md-2">
<div class="ad1">
<img src="<?=$result[0]?>" alt="">
</div>
</div>
<div class="col-md-2">
<div class="ad2">
<img src="<?=$result[1]?>" alt="">
</div>
</div>
<div class="col-md-2">
<div class="ad2">
<img src="<?=$result[2]?>" alt="">
</div>
</div>
<div class="col-md-5">
<div class="ad3">
<img src="<?=$result[3]?>" alt="">
</div>
</div>
</div>

Categories