Can access detailed information through URL with function parameters (Codeigniter) - php

I am creaiting my first Codeigniter application for a blog with news. In the main page there is only the title of the news which is also a link to a view with the detailed information and body of the news. Im having trouble on accessing the get through the URL that has to go over a function that receives the ID of the new as a parameter. I just can get that to work. Can someone help me?
The problem is not in the function itself because it works fine when i assig an static value to the URL, but for some reason i can send the $row->id object as a Get through the URL with the proper value for each of the news.
MODEL
class Post extends CI_Model{
public function getPost(){
$this->load->database('fintech_blog');
$data = $this->db->get('post');
return $data->$result();
}
CONTROLLER
public function getPost($id){
$query = $this->db->query("select * from post where id = '$id' ");
$rows = $query->result(); //method for putting into an array format
$data=array('result'=>$rows);
$this->load->view('view',$data);
}
VIEW
foreach ($result as $row):
$id = $row->id;
$post = site_url('welcome/getPost/$row->id');
?>
<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="post-preview">
<a href="<?php echo $post; ?>">
<h2 class="post-title">
<?php echo $row->title; ?>
</h2>
<h3 class="post-subtitle">
<?php echo $row->calling; ?>
</h3>
</a>
<p class="post-meta">Posted on
<!-- Start Bootstrap -->
<?php echo time_elapsed_string($row->created); ?></p>
</div>
<hr>
</div>
</div>
</div>
<?php endforeach; ?>

The problem comes from the way you create the string for $post.
As one commenter suggests the problem is the use of single quotes and to use double quotes instead. The most important feature of double-quoted strings is the fact that variable names will be expanded. Meaning the variable symbol ($test in this case) is replaced with the value of the variable.
To illustrate:
$test = "blue";
//first, a string created with single quotes
echo 'The sky is $test today.'; //outputs: The sky is $test today.
But using double quotes to create the string...
echo "The sky is $test today."; //outputs: The sky is blue today.
The var $test get expanded to the value it holds.
So, the first two lines of the foreach loop in the view should be.
$id = $row->id;
$post = site_url("welcome/getPost/$row->id");
However, you never use $id and you only use $post once. That says to me that these two lines are not needed. Delete them both.
Change the line
<a href="<?php echo $post; ?>">
to
<a href="<?php echo site_url("welcome/getPost/$row->id"); ?>">

Related

Create CSS Cards from PHP array?

Anyone knows how to do create multiple cards looping through a PHP array?
For example, I have 5 friend with 5 description corrresponding each friend (from a mysqli table) saved in $friendList.
So I want, for each row, to create and show a card with the friend as a header and its description as the content of the card.
This would be the loop
while ($row = mysqli_fetch_array($friendList, MYSQLI_ASSOC)){
// $row['friend'];
// $row['description'];
}
but then, I do not know how to create the cards with the variables obtained:
Assuming that you have $friendList variable already defined and it is a mysqli_result object, here's how you can do it:
<?php while($row = mysqli_fetch_array($friendList, MYSQLI_ASSOC)): ?>
<div class="w3-card-4 test">
<img src="img_avatar3.png" alt="Avatar">
<div class="w3-container">
<h4><b><?php echo $row["friend"] ?></b></h4>
<p><?php echo $row["description"] ?></p>
</div>
</div>
<?php endwhile; ?>
Feel free to ask any questions :-)

Why does assigning a PHP function to a variable execute it?

I am using this in WordPress:
<?php if ( !is_front_page()) {
$title = the_title();
echo <<<EOT
<div class="featured-header col-xs-12 col-sm-12 col-md-12 col-lg-12 cf">
<span class="featured-title">$title</span>
</div>
EOT;
} ?>
However, the page title is generated by the PHP BEFORE the div. It looks like the declaration of the $title variable itself is executing the the_title() function.
Why is this?
EDIT:
Thanks for the explanation! Here is the working code now:
<?php if ( !is_front_page()) {
$title = get_the_title();
$thumbnail = get_the_post_thumbnail_url();
echo <<<EOT
<div class="featured-header col-xs-12 col-sm-12 col-md-12 col-lg-12 cf" style="background-image: url('$thumbnail');">
<span class="featured-title animated fadeIn">$title</span>
</div>
EOT;
} ?>
the_title() is a template function that display (echo) the title. If you want to store the title in a var, use get_the_title() instead.
Note that most of WP template functions work like this : get_the_content() vs the_content(), get_the_date() vs the_date()...
The the_title() function is executed, because you call it. The return value is stored in the $title variable and it's not changed afterwards.
I don't know what the function does. Maybe it echoes something and doesn't return - then the echoed valued will be just printed out, not stored in the variable and you cannot do anything about it.
If it returns something, not echoing, then you can do this:
$heredoc = <<<EOT
<div class="featured-header col-xs-12 col-sm-12 col-md-12 col-lg-12 cf">
<span class="featured-title">%t</span>
</div>
EOT;
$heredoc = sprintf($heredoc, the_title());
echo $heredoc;
Writing $title = the_title() actually calls the function and then assign the returned value, that is normal and is standard in all programming languages. If you want to assign the function itself, you need to assign the name of the function like this :
$title = 'the_title'
And then, at the place where you want the function to be called you just write :
$title()
Which will call the function normally.
As a rule of thumb, you can say that if something is followed by () it will be called immediatly.
source

Add additional queries to url by link

I have a site that uses query strings to retrieve the data like so:
<div id="menu-sort-dropdown" class="search-filter-item">
<p><?php echo $query_sort_title; ?></p>
<ul class="dropdown-menu">
<li>Newest</li>
<li>Oldest</li>
<li>Alphabetically</li>
</ul>
</div>
<div id="menu-category-dropdown" class="search-filter-item">
<p><?php echo $query_category_title; ?></p>
<ul class="dropdown-menu">
<li>All</li>
<li>Coaching</li>
<li>Conversation</li>
<li>Craft</li>
<li>Creativity</li>
</ul>
</div>
It works great getting the data like:
teachings/?sort=SORT_NAME_ASC
or
teachings/?category=Creativity
but I can do both like:
teachings/?category=Creativity&sort=SORT_NAME_ASC
I can't wrap my head around how to add that. If I just append the strip it will become a mess.
The following code doesn't 'duplicate' the values in the url if you keep clicking the category or sort. It's made to copy/paste and run.
// to check the values
echo '<pre>';
print_r($_GET);
echo '</pre>';
echo '<hr>';
function foo($type, $value){
$args = $_GET;
$args[$type] = $value; // prevent duplication
return http_build_query($args); // returns new query string
}
?>
SORT_DATE_LIT_ASC
<br>
SORT_NAME_ASC
<br>
Coaching
<br>
Conversation
You can also have a code to remove any of those. Add it right after the previous code (also copy/paste). Take a look:
<?php
function bar($type){
$args = $_GET;
unset($args[$type]);
return http_build_query($args); // returns new query string
}
?>
<hr>
Remove SORT
<br>
Remove CATEGORY
As for now your dropdown element do a simple action - go to provided url. If you want to select a few values then your elements should store selected value instead of open url. You can do this with for example this JS code
var menus = [];
function addElement(type, element) {
menus[type] = element;
}
The type above is number index of your menu type. For example 0 can be for sort and 1 for category - this is for ensure that you can select only one value from menu type. Now you can replace your code with something like this
<div id="menu-sort-dropdown" class="search-filter-item">
<p><?php echo $query_sort_title; ?></p>
<ul class="dropdown-menu">
<li>Newest</li>
<li>Oldest</li>
<li>Alphabetically</li>
</ul>
</div>
<div id="menu-category-dropdown" class="search-filter-item">
<p><?php echo $query_category_title; ?></p>
<ul class="dropdown-menu">
<li>All</li>
<li>Coaching</li>
<li>Conversation</li>
<li>Craft</li>
<li>Creativity</li>
</ul>
</div>
To go to the prepared URL you need to add another element like for example a button (or something else with calling openUrl function after mouse click)
<input type="button" onclick="openUrl('?')" value="Go to URL">
And the JS code for openUrl function
function openUrl(prefix) {
var url = prefix + menus.join('&');
document.location.href = url;
}

How to work with if not in PHP?

I want to print the div with the class called inner-content-div, if the variable $name is not consisted with the following strings.
Gingelly Rolls
Kithul Treacle
Coconut Vinegar
But my PHP code is not working.
Here is my code.
<?php
$vid = explode("/", $_GET["q"]);
$name = taxonomy_term_load($vid[2]);
?>
<h1 id="page-title" class="title"><?php print $name->name; ?></h1>
<?php
$exclude_list = array("Gingelly Rolls","Kithul Treacle","Coconut Vinegar");
if(!in_array($name, $exclude_list)){ ?>
<div class="inner-content-div">
<!-- Here are some HTML code.-->
</div>
<?php
}
?>
It should be -
if(!in_array($name->name, $exclude_list)){
as you are printing it -
<h1 id="page-title" class="title"><?php print $name->name; ?></h1>
<?php
$vid = explode("/", $_GET["q"]);
$name = taxonomy_term_load($vid[2]);
$exclude_list = array("Gingelly Rolls","Kithul Treacle","Coconut Vinegar");
?>
<h1 id="page-title" class="title"><?php echo $name->name; ?></h1>
<?php if(!in_array($name->name, $exclude_list)): ?>
<div class="inner-content-div">
<!-- Here are some HTML code.-->
</div>
<?php endif; ?>
It could be because $name is an object. Also, probably better to use PHP's alternate syntax for cleaner html/templates.
The other potential issue here is that the casing of the items in the exclude list could potentially not match the value of name. You should probably normalize those to all lower case when doing the comparison.
you can try like this. First print $vid[2], using echo $vid[2];. Then put $vid[2] values into the array called $exclude_list. Then use the following code.
$exclude_list = array(//value 1, value 2, value 3);
if(!in_array($vid[2], $exclude_list)) {
//Your HTML code
}

Data attribute value from MySQL

I'm new to PHP and MySQL so i'm not quite sure what i am doing wrong here. I am using the jQuery plugin quicksand to create a filterable portfolio. The plugin uses custom data attribute data-tag inside the li item to populate the filter nav.
What I am trying to do is use a foreach loop to populate the contents of the ul. The problem is that the filter nav won't auto populate when I use php to get the value of the data attribute from the gallery_tag column in my mySQL table.
<?php
$pagetitle = "Red Penguin - Our Work";
$navcurrent = "work";
$headTitle = "RECENT WORK";
$headsubTitle = "SOME OF OUR RECENT WORK";
include_once('includes/headersub.php');
include_once('includes/connection.php');
include_once('includes/project.php');
$project = new Project;
$projects = $project->fetch_all();
?>
<div class="row">
<nav id="filter"></nav>
<section id="container">
<ul id="stage" class="three-up">
<?php foreach($projects as $project) { ?>
<li class="gallerylist" data-tag="<?php echo $project['gallery_tag']; ?>">
<a href="project.php?id=<?php echo $project['gallery_id']; ?>">
<img src="<?php echo $project['gallery_thumb']; ?> " alt="<?php echo $project['gallery_proj']; ?>" />
<?php echo $project['gallery_title']; ?>
</a>
</li>
<?php } ?>
</ul>
</section>
The error that comes up in the log is in the jquery line:
tags = elem.data('tags').split(',');
The log comes back with this error: "Uncaught TypeError: Cannot call method 'split' of undefined" for the above line.
I'm not quite sure why this is a conflict that causes the jquery to be unable to read the value of the data-attribute as taken from the gallery_tag column of my table. Any help would be appreciated.
I guess there is a spelling mistake in your html you have data-tag and you are trying to get the elem.data('tags') it should be elem.data('tag')
your elem.data('tags') returns undefined. So split() would not work on it.
Maybe I'm wrong, but I think you need: tags = elem.data('tags').split(' ');

Categories