Quick Question!
In redbean, seeing as you can order content like this with the findAll:
$all = R::findAll('needle',' ORDER BY title LIMIT 2 ');
It is possible to order a load function? I have tried but it doesn't work and the previous one does. This is what I tried:
$vals['events'][] = R::load('events', $eventid, ' ORDER BY title LIMIT 2 ');
Thanks!
If you are going to update the events then loading the event is perfectly fine, if you just want to get the event information i would say use find which is better.
<?php
$events = R::findAll('needle');
foreach($events as $e_info){
$eventid = $e_info->id;
$event_info = R::findOne('event_info', ' id = ?' , array($eventid));
//Do whatever you want with the data.
//You can also update it like
// $event_info->updated_at = R::isoDateTime();
//R::store($event_info);
}
?>
I hope this helps.
Related
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. :)
So my brain is having trouble functionning this morning and I can't figure out how to create a Top list.
I have my query like so
$moyennepisode = $conn->prepare('SELECT * FROM show_moyenne, shows WHERE shows.id = show_moyenne.show_id ORDER BY moyenne DESC LIMIT 100');
$moyennepisode->execute();
while ($show = $moyennepisode->fetch()) {
somecode }
Which is all fine but I simply want to output the position so I tried a for loop but I can't seem to figure out where to place it. I tried an associate array but I failed at that, too.
Can anyone recommend how to do that ? And if there might be a way to do have the position from mmysql instead of php ?
Any help appreciated. Thanks!
$position = 1;
while ($show = $moyennepisode->fetch()) {
Output $position;
$position++;
somecode;
}
I am trying to dynamically load more posts into my system
This is my code...
$stmt = "
SELECT * FROM `acmPosting`
WHERE (`sender`='$thisID' AND `postType`='a')
OR (`recip`='$thisID' AND `sender`='$userID' AND `postType`='a')
OR (`sender` IN ($friendsArray) AND `recip`='$thisID' AND `postType`='a')
ORDER BY `timeSent` DESC LIMIT $startlimit,10";
if($stmtCount = $conn->query($stmt)){
if($stmtCount->fetchColumn() > 0){
$result = acmPosts($conn, $site, $userID, $stmt);
$jsonArray['a'] = $result;
$jsonArray['b'] = 'go';
}else{
$jsonArray['a'] = '<div class="thisOutput" style="padding:12px;">There are no more posts</div>';
$jsonArray['b'] = 'stop';
}
}
Everything works fine until it gets to the last set of posts AKA if the LIMIT is 100,10 but there are 105 posts in the call it won't call any to the fetchColumn().
I hope this question makes sense. Thanks in advance for any help you can give.
EDIT
How can I determine when I have reached the LIMIT and act accordingly
Your description is not very precise but maybe this is what you are looking for:
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
I've an issue, which is about php syntax/mysql in drupal:
Let's say that userA has created a content type called "test" where he filled the field field_example with value "xxx". Afterwards, another user, userB has created another content and filled the same field field_example with the same value "xxx".
I'd like to know how is it possible to display a view only with the node created where the field field_example is the same for the current user ? I don't have (and i don't want) a user reference in the content type "test" i'm using.
I've looked through View PHP Filter, but i'm wondering how to compare values of field ? Here's my attempt [i'm not an expert in PHP as you'll might notice :) ] :
<?php
$a="SELECT uid FROM users WHERE uid=%d";
/* ??? How to create $b which can get value of field_example from content_type_test from current user which is logged in ? */
$b="";
$c="SELECT field_example FROM content_type_test";
if ($b==$c){
echo "Ok, I've what I want :) ";
}
?>
Any help would be greatly appreciated since it's been a while i'm looking for information about this query...
Thanks all :)
I don't have a Views module solution.
One thought that comes to mind is what if User A submits multiple nodes, which example value would you use then? Also, what version of Drupal are you working with?
Assuming that a user will only ever submit one content of this type and you are running Drupal 6 (my guess from code examples), then it might look something like this:
<?php
// current user
global $user;
// select this user's node
$nid = db_result(db_query("SELECT nid FROM {node} WHERE type = 'my_content_type' AND status = 1 AND uid = %d", $user->uid));
// if this node loads fine, then proceed with the rest
if ($node = node_load($nid)) {
// find nodes with the same example value, which do not belong to the current user
$result = db_query("SELECT nid FROM {node}
INNER JOIN {content_type_test} ctt ON n.vid = ctt.vid
WHERE n.status = 1 AND ctt.field_example_value = '%s' AND uid <> %d
ORDER BY n.created DESC", $node->field_example[0]['value'], $user->uid);
// loop through results
while ($row = db_fetch_object($result)) {
$node = node_load($node->nid);
// append the teaser output (if this is what you want to do)
$output .= node_view($node, TRUE);
}
// print the output
print $output;
}
else {
print t('No other content found.');
}
?>
If users submit more than one of those content types, then that would have to be another answer to avoid from writing a novel here. There's a couple ways to approach that.
Also if this was Drupal 7, I'd be using different functions as well.
I assume you want something like this:
function _get_list() {
global $user; // This is global variable, contains information for current logged in user.
$results = db_query("SELECT ctt.field_example FROM {node} n JOIN {content_type_test} ctt ON n.nid = ctt.nid AND n.vid = ctt.vid WHERE n.uid = %d", $user->uid);
while($row = db_fetch_object($results)){
//$row->nid.. etc..
}
}
I'm making a related stories section which uses the tags in a post to go through and find other stories with similar tags.
I want to make sure that I'm not pulling the same story multiple times if it shares more than one tag with another post.
So it's basically
foreach($tags as $t) {
$getStories = mysql_query("SELECT * FROM `posts` WHERE `tags` LIKE '%$t%' LIMIT 2");
while($related = mysql_fetch_array($getStories)) {
echo $related['title'];
}
So I pull 2 related stories based on the first tag, now, when it goes around through next loop for the second tag, how can I make sure that a story pulled the last time doesn't get picked the second or third time. I do have a unique ID just called 'id', just not sure what to do with it in this situation.
Thanks!
What you could do is fetch all related articles at once:
$tagsClause = '';
foreach ($tags as $t) {
$tagsClause .= " OR tags LIKE '%$t%'";
}
$tagsClause = substr($tagsClause, 4); // Remove first ' OR '
$getStories = mysql_query('SELECT * FROM `posts` WHERE ' . $tagsClause);
while($related = mysql_fetch_array($getStories)) {
echo $related['title'];
}
However, this does not account for your use of LIMIT.
EDIT
Apparently this did not pose a problem, cf. comments.
Use UNIQUE.
SELECT UNIQUE(post_id), [other stuff] FROM posts WHERE [...]
i think you can make an array containing the ids you have selected, when you process a tag, check the id is not in the selected array, but it's not efficient.
just like this:
$ids = array();
foreach($tags as $t) {
$getStories = mysql_query("SELECT * FROM `posts` WHERE `tags` = '$t' LIMIT 2");
//here to check and add id to array ids
while($related = mysql_fetch_array($getStories)) {
echo $related['title'];
}
}