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']}'>
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 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
I have 2 fields in a mysql database. They are title which is the news article title and (description/link) which is the link to the article. The web page is http://vince.netau.net .Type bitcoin in the search field. I would like to be able to click on the title and bring up the link to the article. I don't want to even see the description column with the link like it is currently.
Here is the code I have currently which has the Title with the link but I'm showing the link in the next column which I don't want. Also, is there a way to not have the title underlined?
<td><a href="<?php echo $row['description']; ?>"
<td><?php echo $row['post_title']; ?></td>
Thanks for the help!!!!
You did not close tags correctly. Try this, they will be in same column
<td><a href="<?php echo $row['description']; ?>">
<?php echo $row['post_title']; ?></a></td>
I hope this helps
Edit:
Also for not underlined link you can use this css attribute:
<td><a style="text-decoration: none;" href="<?php echo $row['description']; ?>">
<?php echo $row['post_title']; ?></a></td>
Your html code is not really correct. You have to close all tags and you should read about HTML and the HTML structure.
<td>
<a href="<?php echo $row['link']; ?>">
<?php echo $row['post_title']; ?>
</a>
</td>
Use link instead of description.
Using css you can remove the underline:
<td>
<a href="<?php echo $row['link']; ?>" style="text-decoration: none;">
<?php echo $row['post_title']; ?>
</a>
</td>
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);?>
">
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?