create dynamic link from Mysql table - php

Im trying to create dynamic links from some post inside a database table,
but i cant figure out how to create the link, when the user is already logged in.
I think something like this.
<?php $articles = new Articles();
foreach($articles->fetch_user_article($_GET['uid']) as $article) :?>
<?php echo $article['title'];?>
<?php endforeach ?>
This gives me a link that looks like this
edit_articles.php?uid=5&article=213
The article id:s are correct from the DB table.
Now my edit_articles.php file
$articles = new Articles();
$article = $articles->fetch_user_article($_GET['uid']);
echo $article['text'];
But when im reach the edit_articles.php file i get
Undefined index: text
And my function
function fetch_user_article($uid){
$uid = (int)$uid;
$query = $this->link->query ("SELECT id, title,text FROM blog WHERE user_id = '{$uid}' ");
$tweet = array();
while(($row = $query->fetch(PDO::FETCH_ASSOC)) !== FALSE) {
$tweet[] = $row;
}
return $tweet;
}

Your function fetch_user_article is returning more than one article.
Use like this to fecth all articles.
$articles = $articles->fetch_user_article($_GET['uid']);
foreach ( $articles AS $article ) {
echo $article['text'];
}
If you want fetch_user_article to return only one article, then the field user_id of table blog should be unique.
Or you will have to rewrite the query
SELECT id, title,text FROM blog WHERE user_id = '{$uid}'
so it gives you only one result, something like:
$article_id = $_GET['article'];
SELECT id, title,text FROM blog WHERE id = '{$article_id}'

Related

How to get a hyperlink to work from database

on my website I currently have a save to 'favorites' button which saves a recipe to the users favorites and displays their favorites on their dashboard. I have managed to display the favorites on their dashboard with hyperlinks surrounding them. However I can't quite figure out how to actually connect each favorite to its link.
Below is the code to retrieve the favorites stored on the database.
$favs = array();
$links = array();
$sql = "SELECT * FROM recipe WHERE fav='yes'";
$records = mysql_query($sql);
while($result =mysql_fetch_assoc($records)){
$favs[] = $result['recipeName'];
$links[] = $result['url'];
}
I have also saved the url of each recipe on my database, so just need to print out each favorite with the link to their url.
below is the code to display the favourites surrounded by a tags. I have also managed to return the url links from the database but just need to conect them to each recipe.
foreach ($favs as $fav) {
echo '<a href=''>'.$fav.'</a>'.' ';
}
You don't need foreach loop and opening blank arrays here:
while($result =mysql_fetch_assoc($records)){
echo 'Go to '.$result['recipeName'].'';
}
I suggest you put the recipeName and the URL in a single array.
e.g.
$i = 0;
while($result =mysql_fetch_assoc($records)) {
$fav_links[i] = array('recipeName' => $result['recipeName'], 'url' => $result['url']);
}
Then in your foreach:
foreach ($fav_links as $fav) {
echo ''.$fav["recipeName"].''.' ';
}

How to create multidimensional array based on ID from each row (in same View) Codeigniter

I am new in codeigniter both php, I am beginner. Could anyone help me how to show a data from another table based on ID in each row in same View in codeigniter, i just include a file(that is a form with data from another table) im not sure.. how the method should be.. dont know what to do plz ! thx
CONTROLLER
$this->load->model('m_work_order');
$this->load->model('m_srv_product');
$data['items'] = $this->m_work_order->show_items()->result();
$id=0; // dont know what to do with this ID
$data1['record'] = $this->m_srv_product->show_data($id)->result_array();
$data['form'] = $this->load->view('work_order/form_file', $data1, TRUE);
$this->load->view('work_order/form_input',$data);
MODEL m_work_order
function show_items(){
$query = "SELECT *
FROM work_order_items
WHERE item_status='0'";
return $this->db->query($query);
}
MODEL m_srv_product
function show_data($id)
{
$query= "SELECT a.srv_pr_id,a.srv_vol,a.srv_n,
b.srv_name,c.prod_cat_name,c.prod_cat_id
FROM srv_pr_use as a,service as b, prod_category as c
WHERE a.srv_id = b.srv_id and
a.prod_cat_id = c.prod_cat_id and
a.srv_id = $id ";
return $this->db->query($query);
}
VIEW
foreach ($items as $t){
echo "
<tr>
<td>$no</td>
<td>$kode</td>
<td>$t->item_name</td>
<td>$t->item_qty</td>
<td>".$form."</td>
</tr>"
}

Laravel PHP: Single query to fetch User, UserPosts and PostComments

I'm new to Laravel and MySQL, i couldn't figure out how to use a single join query to get user, their posts, and post comments. I'm not sure if i'm doing it right, but there must be a better way to fetch posts and post comments in a structured way. This is what I've done so far. I'm using $username to lookout for every single post user has made in Posts table and then looping through each post to see if that specific post has any comments with post.id.
It's working perfectly, but i feel that it's a bit expensive way to loop through each single posts to find it's comments with an individual query. Is there a better way to do it in a single query and keep data structured so that i can easily populate my view?
public function user($username){
$user = User::where('username',$username);
if($user->count()){
$user = $user->first();
$posts = Posts::where('username', $username)->orderBy('created_at', 'DESC')->orderBy('at','DESC')->paginate(4);
if(count($posts)>0){
$i=0;
foreach($posts as $data){
$ago = date('Y-m-d', strtotime($data->created_at)).' '.$data->at;
$comments = Comments::where('post_id',$posts[$i]->id)->get();
$posts[$i]->at = ProfileController::getElapsedTime($ago);
$posts[$i]->comments = $comments;
$j=0;
foreach($posts[$i]->comments as $comments){
$c_time = date('Y-m-d', strtotime($comments->created_at)).' '.$comments->at;
$posts[$i]->comments[$j]->at = ProfileController::getElapsedTime($c_time);
$comment_user = User::where('username', $posts[$i]->comments[$j]->comment_user)->first();
$posts[$i]->comments[$j]->user_dp = $comment_user->dp_uri;
$posts[$i]->comments[$j]->name = $comment_user->name;
$j++;
}
//echo $posts[$i]->comments.'<br><br>';
$i++;
}
}
return View::make('profile.posts')
->with('user',$user)->with('posts', $posts);
}else{
return Response::view('errors.missing', array(), 404);
}
}

PHP - Loop and not loop at the same time from a join

I'm trying to display a company name from a company table, and then loop out news from another table. The two tables are joined. This means that: I want to just pick out one row from one table in the join, and loop out data from the other table in the join. Is this possible? My code below only display two out of three posts in the news table in my loop.
Thanks!
$sql = "SELECT
newID, newTitle, newSummary, newDate,
comID, comName, comImageThumb
FROM
tblNews a RIGHT JOIN tblCompanies b
ON a.newCompanyID = b.comID
ORDER BY newDate DESC";
// Get company name and display
$companyData = mysql_fetch_assoc($result);
$comName = $companyData['comName'];
echo "<a href='#' class='name'>$comName</a>";
// Looping news
while($news = mysql_fetch_assoc($result)) {
// Display news posts
$newTitle = $news['newTitle'];
echo $newTitle;
}
Put everything inside while loop and try :
while($news = mysql_fetch_assoc($result)) {
// Get company name and display
$comName = $news ['comName'];
echo "<a href='#' class='name'>$comName</a>";
// Looping news
// Display news posts
$newTitle[] = $news['newTitle'];
echo $newTitle;
}
You should look at mysql GROUP_CONCAT for more detail.
Easiest solution probably would be to keep track of whether you are in the first iteration:
// query company name and news entries here
$firstRow = true;
while($news = mysql_fetch_assoc($result)) {
if ($firstRow) { echo $news['comName']; }
$firstRow = false;
echo $news['newTitle'];
}
You could also keep your current code and use mysql_data_seek(0) to reset the cursor after extracting the company name first.
And on a side note: Don't use mysql_* functions anymore! Use PDO instead.
Move the fetch operation to the end of the loop:
$companyData = mysql_fetch_assoc($result);
$comName = $companyData['comName'];
echo "<a href='#' class='name'>$comName</a>";
// you've already fetched a news item....
$news=$companyData;
do {
$newTitle = $news['newTitle'];
echo $newTitle;
} while ($news=mysql_fetch_assoc($result));

(Redbean) Unable to access linked table values

I am testing out the Redbean ORM. I like how it works, less work for me :) I am using the redbean books example, from their website. I have been able to create new books with authors and titles, and display them to the page with no problem. I added another dimension to learn how to link pages to my books.
I am able to add an entry into the book table, as well as add an entry into the page table using the following code:
----------- dbmgmt.php ---------------------------------
function AddNewBook($FORMINFO){
$newBook = R::dispense('book');
$newBook->title = $FORMINFO['title'];
$newBook->author = $FORMINFO['author'];
$newBook->create_date = R::isoDateTime();
$newPage = R::dispense('page');
$newPage->pagetext = $FORMINFO['pagetext'];
$newBook->ownPage = $newPage;
$id = R::store($newBook);
return R::load('book', $id);
}
Both tables show the proper entries (new ids and populated fields). However, I am having a hard time accessing the pagetext field from the page table. This is the code I have been using for that:
----------- dbmgmt.php ---------------------------------
function GetBooks($id){
if($id == ""){
return R::find('book');
}
else{
return R::find('book','id = ?', array($id));
}
}
----------- GetBooks.php ---------------------------------
$id = "";
if(isset($_GET['id'])){
$id = $_GET['id'];
}
$books = GetBooks($id);
$booklist = "";
foreach($books as $book){
$pagetext = "";
foreach($book->ownPage as $page){
$pagetext .= $page->pagetext; //Errors with "Notice: Trying to get property of non-object"
}
$booklist .= "<tr id='$book->id'><td><a class='linkEdit' href='edit.php?id=$book->id'><img src='http://cdn1.iconfinder.com/data/icons/ledicons/page_white_edit.png' /></a> <span class='book-title'>$book->title</span></td><td><span class='book-author'>$book->author</span></td><td><span class='page-text'>$pagetext</span></td></tr>";
}
echo $booklist;
------------------------------------------------------------
When I print_r($book->ownPage), I don't even see the page entry that I can clearly see in the page table via phpmyadmin. I have tried many different ways of accessing the pagetext field via my script above, but can't get anywhere with it.
Any insight would be most welcome. Otherwise I will have to try a different, albeit bigger and more cumbersome, ORM.
Thanks in advance.
I am not 100% sure as I haven't encountered this problem yet myself, but I am almost positive, $book->ownPage needs to be an array:
$newPage = R::dispense('page');
$newPage->pagetext = $FORMINFO['pagetext'];
$newBook->ownPage[] = $newPage;
$id = R::store($newBook);

Categories