Codeigniter data from view to controller? - php

New to Codeigniter and trying to teach myself by exploring and messing with InvoicePlane however i'm a bit stuck when it comes to this section.
I've got a controller which loops through a number of different styles. To sends it to index.php view and is displayed fine.
Each style might have a parent style and again I can display the parent style ID fine but i'm trying to get my head around how to get the name of each parent style from the ID.
For example, the view page is as follows:-
<?php foreach ($property_styles as $style) { ?>
<tr>
<td><?php echo $style->property_style_id; ?></td>
<td><?php echo ($style->property_style_group) ? lang('yes') : lang('no'); ?></td>
<td><?php echo $style_parent->property_style_name; ?></td>
<td><?php echo $style->property_style_name; ?></td>
<td class="text-right">
<a href="<?php echo site_url('property_styles/form/' . $style->property_style_id); ?>"
title="<?php echo lang('edit'); ?>"><i class="fa fa-edit orange fa-margin"></i></a>
</td>
</tr>
<?php } ?>
The third table cell hows - $style_parent->property_style_name which is where i'd like the name to appear. The value it needs to use to determine is $style->property_style_parent_id
Could someone help point me in the right direction?

Related

Codeigniter 4 : "Attempt to read property 'name' on array" error when executing ->findAll() on the BaseBuilder

this is my first time exploring Codeigniter 4 and I follow some tutorials to understand the basic crud process.
Below are my controller where the model query is used.
<?php
namespace App\Controllers;
use CodeIgniter\RESTful\ResourcePresenter;
class Project extends ResourcePresenter
{
protected $modelName = 'App\Models\ProjectModel';
public function index()
{
return view('projects/index', ['projects' => $this->model->orderBy('created_at', 'asc')->findAll()]);
}
}
Then below are the index page where I want to display the list and the part where I have a problem.
<?php foreach ($projects as $project) : ?>
<tr>
<td><?= $project->name ?></td>
<td><?= $project->description ?></td>
<td>
<form action="<?php echo base_url('/projects/delete/' . $project->id); ?>" method="post">
<a class="btn btn-outline-info" href="<?php echo base_url('/projects/show/' . $project->id); ?>">
Show
</a>
<a class="btn btn-outline-success" href="<?php echo base_url('/projects/edit/' . $project->id); ?>">
Edit
</a>
<button type="submit" class="btn btn-outline-danger">Delete</button>
</form>
</td>
</tr>
<?php endforeach; ?>
'name' and 'description' are the column names stored in the database.
Can anyone help me check whether the problem is because of code or another external factor since my friend who tried the same tutorial succeeded even though the code is the same?
Below are the errors that show up.
->findAll() returns an array of elements.
Instead of: ❌
<td><?= $project->name ?></td>
<td><?= $project->description ?></td>
Use this: ✅
<td><?= $project['name'] ?></td>
<td><?= $project['description'] ?></td>
Do the same for other sections. I.e :$project->id.

Printing URL links within a function

I am creating a movie database. I need View, Edit, and Delete to link to other pages within my site. For example, I want view to go to a link called show.php but I need the page to dynamically load with the $movie[id] information. How can I do that from within the function. Here's what I have so far:
printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
$movie['id'],
$movie['title'],
$movie['director'],
$movie['year'],
printDropdown ($movie['cast']),
'<img src="'.$movie ['poster'].'" height="100" width="75"/>',
'<a class="action" href="">'.'View'.'</a>',
'<a class="action" href="">'.'Edit'.'</a>',
'<a class="action" href="">'.'Delete'.'</a>',
);
}
Edited on 5/12:
I decided to take a different approach. The poster column appears in the table but it shows that all the images are broken even though the URL image source is correct for each array. What am I missing from the table column that contains the image source?
<?php foreach($movies as $movie) { ?>
<tr>
<td><?php echo h($movie['id']); ?></td>
<td><?php echo h($movie['title']); ?></td>
<td><?php echo h($movie['director']); ?></td>
<td><?php echo h($movie['year']); ?></td>
<td><?php echo printDropdown($movie['cast']); ?></td>
<td><img src="'.$movie['poster'].'" height="100" width="75"/></td>
<td><a class="action" href="<?php echo url_for('show.php?id=' . h(u($movie['id']))); ?>">View</a></td>
<td><a class="action" href="<?php echo url_for('edit_movie.php?id=' . h(u($movie['id']))); ?>">Edit</a></td>
<td><a class="action" href="">Delete</a></td>
</tr>
<?php } ?>
</table>
Read up on PHP GET. And you should be able to do it.
You have to do something like this:
printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
$movie['id'],
$movie['title'],
$movie['director'],
$movie['year'],
printDropdown ($movie['cast']),
'<img src="'.$movie ['poster'].'" height="100" width="75"/>',
'<a class="action" href="show.php?action=view&id=' . $movie['id'] . '">'.'View'.'</a>',
'<a class="action" href="">'.'Edit'.'</a>',
'<a class="action" href="">'.'Delete'.'</a>',
);
Within the other page. like show.php you can get the given action and id like:
$id = $_GET['id']; // the given id
$action = $_GET['action']; // view
You can use GET method
In this case you place parameters in the url of the href attributes. Like:
<a href='<url>?movie_id={$movie['id']}&title={$movie['title']}'>

Codeigniter send GET parameter with site_url?

I have this part of code in my view file:
<?php foreach ($posts as $post) { ?>
<tr>
<td><?php echo $post['id']; ?></td>
<td><?php echo $post['title']; ?></td>
<td><?php echo $post['content']; ?></td>
<td>
<div class="btn-group">
Edit
Delete
</div>
</td>
</tr>
<?php } ?>
Well .. how to send GET parameter with site_url function, this is my anchor part:
a href="<?php echo site_url('admin/blog/show'); ?>" class="btn btn-success">Edit</a>
where to send $post['id'] ?
I see some question in SO, but answers was with hardcode...
here we have a example
echo anchor('news/local/'. $post['id'], 'any text', array('title' => 'The best news!'));
In your controller you will get something like.
echo $this->uri->segment('3');
Also load url helper/library as well.
Should be like this :
Edit
The given code should be like this :
<?php foreach ($posts as $post) { ?>
<tr>
<td><?=$post['id']; ?></td>
<td><?=$post['title']; ?></td>
<td><?=$post['content']; ?></td>
<td>
<div class="btn-group">
Edit
Delete
</div>
</td>
</tr>
<?php } ?>
In your show method access post id like this :
public function show($post_id)
{
echo $post_id;
}
For more : https://www.codeigniter.com/user_guide/helpers/url_helper.html

How to get same row data when click on button using PHP?

I need to fetch one row data from array using PHP. Here is my code:
<?php
if($bstatus==1) {
$bikeArr=$data['data'];
$count=1;
foreach ($bikeArr as $v) {
?>
<tr>
<td><?php echo $count++; ?></td>
<td><?php echo $v['book_date']; ?>, <?php echo $v['book_time']; ?></td>
<td><?php echo $v['book_id']; ?></td>
<td><?php echo $v['start_date']; ?>, <?php echo $v['start_time']; ?> /<br /> <?php echo $v['end_date']; ?>, <?php echo $v['end_time']; ?></td>
<td><?php echo $v['service_name']; ?> / <br /> <?php echo $v['model_name']; ?></td>
<td><?php echo $v['booking_status_str']; ?></td>
<td><a class="btn btn-xs btn-success" data-toggle="modal" data-target="#takemebookingsec">View Booking <i class="fa fa-arrow-circle-right"></i></a></td>
</tr>
<?php
}
}
?>
Here I need while user will click on View Booking button the exact same row index data will fetch from array and push into other variable.
You can parse the value of the primary key on the view booking button as in
<?php
echo $v['whatever_your_primary_key_is'];
?>
And then on the modal, get that id and do a query to get the current details You will need to parse these details as values of course on your html form in the modal view
To avoid using the database again, just echo the value of primary. But make sure you are still in the same loop to be able to get the current values. SO your modal must also be in the same loop
Try:
<a class="btn btn-xs btn-success" data-toggle="modal" data-target="#takemebookingsec<?= $v['your_primary_key_goes_here']; ?>">View Booking <i class="fa fa-arrow-circle-right"></i></a>
On your modal, still in the same loop,
<div class="modal fade" id="takemebookingsec<?=$v['your_primary_key_goes_here']; ?>">
<form>
<input type="text" name="name" value="<?= $v['what_ever_name']; ?>">
<!--Add other input here-->
</form>
</div>

How to pass a row id to a function in the controller in ci

I have displayed a table using database data.Then I wanted to pass the row id to a function in the controller. How can I do that? Its really great if someone can help me.Thanks inadvance.
I used $rid variable for this. But I got an error.
This is the view
foreach($posts as $post){ ?>
<?php $rid = echo $post->r_id;?>
<tr id="<?php echo $post->r_id; ?>" >
<td style="text-align: center"><?php echo $post->Fname; ?></td>
Use . to join String
<a href="
<?php echo base_url('index.php/ManageInquiries_controller/viewSummary/' . $post->r_id);?>
">

Categories