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
Related
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.
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']}'>
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>
After clicking the VIEW button and nothing happening, I started to wonder what's wrong?
GALLERIES NAME POST EDIT DELETE
Slideshow VIEW
SERVICES POST VIEW
Code:
<?php foreach ($gallery as $gallery_item): ?>
<tr>
<td><?php echo $gallery_item['galleries_name']; ?></td>
<td>
<?php if( $gallery_item['galleries_post_type'] == 'post') { echo "#"; }?>
</td>
<td>
<button type="button" class="edit" onclick="location.href = '
<?php
if( $gallery_item['galleries_post_type'] == 'post') {
echo site_url('cpages/galleries/'.$gallery_item['galleries_id']);
} else {
echo site_url('cpages/viewpictures/'.$gallery_item['galleries_id']);
}
?>
';">VIEW</button>
</td>
I think you should use links and describe them as button (role, type, whatsoever) and decore them with CSS. That is:
<?php
$id = $gallery_item['galleries_id']);
?>
<td>
<?php if ($gallery_item['galleries_post_type'] === 'post'): ?>
<a href="<?= site_url('cpages/galleries/'.$id); ?>" class="edit">
<?php elseif (): ?>
<a href="<?= site_url('cpages/viewpictures/'.$id); ?>" class="edit">
<?php endif; ?>
</td>
By searching for a better solution, I found an answer from BalusC that, I think, should solve your problem and help you understand how/what/when questions about buttons, links and so one.
I have a weird problem with Codeigniter and Datamapper,
I am making an CMS system and I am trying to edit an article from the Database using Datamapper. When I click to edit the article I get the message 'You have succesfully saved the article ' without clicking on the SAVE button, also all the data becomes empty and leaves 0. When I enter the data and click on SAVE it saves it but again when I try edit it again it goes back to 0 ... Can someone help out please
Here is my edit function in the controller
public function edit($id = NULL)
{
// Get articles by ID
$articles = new Article_model();
$article = $articles->where('id', $id)->get();
if ($id)
{
$id == NULL || $article;
count($article) || $error = 'Page not found ';
}
else
{
$article = $this->article_model->get_new();
}
$article->title = $this->input->post('title');
$article->text = $this->input->post('text');
if ($article->save())
{
echo 'You have succesfully saved the article';
}
else
{
echo 'Sorry something went terribly worng';
}
$data = array(
'admin_content' => 'admin/article/edit',
'article' => $article,
);
$this->parser->parse('admin/template_admin', $data);
}
and here is my view
<?php if ($this->tank_auth->is_logged_in()): ?>
<div class="container">
<div class="row">
<div class="col-md-9">
<h3><?php echo empty($article->id) ? 'Add an article' : 'Edit a Page ' . $article->title ;?></h3>
<?php echo form_open(); ?>
<table class="table">
<tr>
<td>Publication Date</td>
<td><?php echo form_input('pubdate', set_value('pubdate', $article->pubdate), 'class="datepicker"'); ?></td>
</tr>
<tr>
<td>Title</td>
<td><?php echo form_input('title', set_value('title', $article->title)); ?></td>
</tr>
<tr>
<td>Body</td>
<td><?php echo form_textarea('text', set_value('text' , $article->text), 'class="tinymce"'); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
</tr>
</table>
<?php echo form_close(); ?>
</div>
</div>
</div>
<?php else: redirect('/auth/login'); ?>
<?php endif; ?>
that's not weird at all...every time that controller is loaded it's going to execute the save() method of the article class.
you should probably include some form validation and only save the article if the form was submitted and validation passes.