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);
Related
JoomlaTune - developers of the JComments extension - offers the following code to display comments anywhere:
<?php
$comments = JPATH_SITE . '/components/com_jcomments/jcomments.php';
if (file_exists($comments)) {
require_once($comments);
$options = array();
$options['object_id'] = $this->item->id;
$options['object_group'] = 'com_content';
$options['published'] = 1;
$count = JCommentsModel::getCommentsCount($options);
echo ('<span>'. $count .'</span>');
}
?>
by substituting the id of the article into $this->item->id you can get the number of materials for this article.
Is it possible to somehow adapt this code to display the number of comments left by specific users by his id. Or maybe this variable already exists somewhere in the component code?
Thanks a lot in advance!
all you need to do is add the following code:
use Joomla\CMS\Factory;
$user = Factory::getUser();
$options['userid'] = $user->id;
just before:
$count = JCommentsModel::getCommentsCount($options);
I am currently working on some php code using redbeanphp that should store data into database tables using four different R::store functions, but only the first two work, and I don't know why the other two aren't working because I made no spelling mistakes and if I try to do it with R::storeAll it also only works for the first two objects.
My code:
require "rb.php";
R::setup('mysql:host=localhost;dbname=building_framework', 'root', '');
// Books table
$books1 = R::dispense('books');
$books1->title = "Wonders of the world";
$books1->author_id = "1";
$books1->publisher_id = "1";
$books2 = R::dispense('books');
$books2->title = "Math for dummy's";
$books2->author_id = "1";
$books2->publisher_id = "1";
// Author table
$author = R::dispense('author');
$author->id = "1";
$author->author = "Francesco Boccia";
// Publisher table
$publisher = R::dispense('publisher');
$publisher->id = "1";
$publisher->publisher = "Metro Books";
$store1 = R::store($books1);
$store2 = R::store($books2);
$store3 = R::store($author);
$store4 = R::store($publisher);
echo $store1;
echo $store2;
echo $store3;
echo $store4;
When I execute it, all the tables are getting made, but only in the books table, data is stored and not in the other two. Can someone please help me fixing this?
I know my mistake. I added an id for author and for publisher but the id gets added by redbean itself, so there was no need for that.
I am trying to create a post-comment system, with simple php-mysqli, as simple as it is, it does not seem to give me the result in the fashion I want i.e:
---POST MESSAGE----
-----comments-----
Here is the code I used:
<?php
session_start();
include_once('php_includes/db_conx.php');
$user=$_SESSION['user'];
$o =mysqli_query($db_conx, "SELECT post.id,post.post,post.date,post_comments.poster,post_comments.comment,post_comments.date FROM post LEFT JOIN post_comments ON post.id=post_comments.post_id AND post.username='$user' ORDER BY post.date");
while($r=mysqli_fetch_array($o,MYSQLI_ASSOC)){
$status= $r['post'];
$date=$r['date'];
$com=$r['comment'];
$pid=$r['id'];
$poster=$r['poster'];
if(count($pid) > 1){
}
echo $status.'|'.$pid.'|'.$date.'<br>'.$poster.':'.$com.'<hr>';
}
?>
It seems to duplicate the post for each comment for same post.
Not sure am making sense, but i will appreciate an answer.
First, add post id to your query's ORDER BY. That will ensure that your post and all its comments appear together, and only once. (I'd recommend adding post_comments.date as well so your comments will appear in order, but that won't be necessary to get the grouping working.)
... ORDER BY post.date, post.id, post_comments.date
Then keep track of the post id as you go. Echo the post information only when the post id changes.
$id = null; // initialize to null
while ($r = mysqli_fetch_array($o, MYSQLI_ASSOC)) {
$pid = $r['id'];
$status = $r['post'];
$date = $r['date'];
$com = $r['comment'];
$poster = $r['poster'];
if ($pid !== $id) {
echo $status.'|'.$pid.'|'.$date.'<br>'; // new post, so echo post info here
$id = $pid; // $id becomes new post id
}
if ($com) {
echo $poster.':'.$com.'<br>'; // echo comment if present
}
}
One other thing that will probably cause some trouble is that you have selected both post.date and post_comments.date in your query, so I'm not sure which one will be in $r['date']. It would be a good idea to alias at least one of those columns to disambiguate them.
I know this has been done before. You can see an example of it whenever you post a new blog post/page in Wordpress, and the title is the same title as an existing page/post. Here's an example:
some-page-slug
some-page-slug-1
some-page-slug-2
How would you programmatically (using PHP) deal with someone submitting the slug of "some-page-slug" with the given list. Obviously, you should result in "some-page-slug-3", but what does that code look like? For some reason, this escapes me. I'm assuming, and hopefully I'm wrong, you would have to use jQuery (or vanilla js, whatever), correct?
Here's a possible solution like Mathew MacLeans suggested in his comment:
$slug = 'slug';
$slugs = array('slug', 'slug-1', 'slug-2', 'slug-5');
$result = $slug;
$i = 1;
while(in_array($result, $slugs)) {
$result = $slug . '-' . $i;
++$i;
}
// prints 'slug-3'
print $result;
Of course you have to replace in_array with your function that checks for existence of a slug.
Demo
Try before buy
Just some pseudo-code to get you going, but I believe this is the route you should take.
$postTitle = <WhateverTheTitleIs>;
$result = mysql_query("SELECT id FROM mytable WHERE title = '$postTitle'");
if(mysql_num_rows($result) == 0) {
// title not found, submit to database
} else {
// title exists
$postTitle = $postTitle + 1;
}
Now, obviously this isn't anywhere near 100% correct syntax, but it should more than point you in the direction that you need to go. :)
I am trying to follow help found in this question: how can i return multiple database record from a class in OOP programming but I cannot seem to get it working properly.
I have a database table called 'jobs' and I want to retrieve all the data from that table display it to my users.
As of right now, when I use this code:
/* Get All Jobs */
public function getJobs($page) {
$db = new DBConnection;
$str = new str_format;
$validate = new data_validation;
$sql = "SELECT * FROM jobs ORDER BY job_id DESC";
$paginate = new Paginate($db, $sql, $page);
$result = $paginate->get_results();
$jobArr = array();
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
$row = $validate->escapeArray($row);
$job = new Job($db);
$job->$row['job_id'];
$job->$row['job_title'];
$job->$row['date'];
$job->$row['industry'];
$job->$row['company'];
$job->$row['photo'];
$job->$row['content'];
$jobArr[] = $job;
return $jobArr ;
}//close while loop
/*=== build the pagination links ===*/
echo "<div class='pagination_container'>";
echo "<span class='pagination'>";
echo $paginate->show_pages();
echo "</span>";
echo "</div>";
/*=== end build pagination links ===*/
}
I only get 1 entry and a bunch of errors that look like this Notice: Undefined property: Job::$4 in /home/hirestar/public_html/builder/classes/job.class.php on line 54
The class is named Job if that has anything to do with it. But I cannot seem to get this work properly.
How can I return all the jobs in my database from this function?
First off, you are calling return within your while loop, so it will bail out on the first loop execution. This is why you are only getting one result. You should call return after the while loop so that you can build your entire array.
Second, you are likely getting the Undefined property errors because of this code:
$job->$row['job_id'];
$job->$row['job_title'];
$job->$row['date'];
$job->$row['industry'];
$job->$row['company'];
$job->$row['photo'];
$job->$row['content'];
Here you are probably wanting to do something like:
$job->job_id = $row['job_id'];
Right now what you are trying to do is read the property from $job with a name equal to the value of $row[*]. So say the row_id is 4, you are in essence performing:
$job->4;
which is obviously meaningless.