tags:

views:

72

answers:

2

Hi,

I have a table in MySQL with "text", "date_posted", and "user". I currently query all text from user=Andy, and call those questions. All of the other text fields from other users are answers to the most recent question.

What I want is to associate those answers with the most recent question, with a loop similar to "for each text where user=Andy, find the text where user!=Andy until date>the next user=Andy (question)"

This seems awfully contrived, and I'm wondering if it can be done roughly as I've outlined, or if I can save myself some trouble in how I'm storing the data or something.

Thanks for any advice.

EDIT: I've added in the insert queries I've been using.

$url = "http://search.twitter.com/search.json?q=&ands=&phrase=&ors=&nots=RT%2C+%40&tag=andyasks&lang=all&from=amcafee&to=&ref=&near=&within=1000&units=mi&since=&until=&tude%5B%5D=%3F&rpp=50)";
$contents = file_get_contents($url);
$decode = json_decode($contents, true);
foreach($decode['results'] as $current) {
 $query = "INSERT IGNORE INTO andyasks (questions, date, user) VALUES ('$current[text]','$current[created_at]','Andy')";
 mysql_query($query);
}

$url2 = "http://search.twitter.com/search.json?q=&ands=&phrase=&ors=&nots=RT&tag=andyasks&lang=all&from=&to=amcafee&ref=&near=&within=15&units=mi&since=&until=&rpp=50";
$contents2 = file_get_contents($url2);
$decode2 = json_decode($contents2, true);
foreach($decode2['results'] as $current2) {
 $query2 = "INSERT IGNORE INTO andyasks (questions, date, user) VALUES ('$current2[text]','$current2[created_at]','$current2[from_user]')";
 mysql_query($query2);
}

And then on the SELECT side, this is where I am currently:

    $results = mysql_query("SELECT * FROM andyasks");
$answers = mysql_query("SELECT * FROM andyasks WHERE 'user' != 'Andy'");
while($row = mysql_fetch_array($results))
{
 if ($row['user'] == 'Andy') {
    print(preg_replace($pattern, $replace, "<p>".$row["questions"]."</p>"));
}
}
while($row = mysql_fetch_array($answers))
{
    print(preg_replace('/@amcafee/', '', "<p>".$row["questions"]."</p>"));
}
A: 

If I understand you correctly you want something like:

$myArr = array("bob","joe","jennifer","mary");
while ($something = next($myArr)) {
   if ($nextone = next($myArr)) {
       //do Something
       prev($myArr)
   }

}

see http://jp2.php.net/next as well as the sections on prev, reset and current

Jonathan Fingland
+2  A: 

What you have in mind could, I believe, be done with subtle use of JOIN or nested SELECT, ORDER BY, LIMIT, etc, but, as you surmise, it would be "awfully contrived" and likely pretty slow.

As you suspect, you would save yourself a lot of trouble at SELECT time if you added a column to the table, which, for answers, has the primary key of the question they're answering (that could be easily obtained at INSERT time, since it's the latest entry with user equal Alex). Then the retrieval would be easier!

If you can alter your schema this way, but need help with the SQL, pls comment or edit your answer to indicate that and I'll be happy to follow up (similarly, I'd be happy to follow up if you're stuck with this schema and need the "awfully contrived" SQL -- I just don't know which of the two possibilities applies!-).

Edit: since the schema's changed, the INSERT could be (using form :name to indicate parameters you should bind):

INSERT IGNORE INTO andyasks
  (questions, date, user, answering)
  SELECT :text, :created_at, :from_user,
    IF(:from_user='Andy', NULL, aa.id)
  FROM andyasks AS aa
  WHERE user='Andy'
  ORDER BY date DESC
  LIMIT 1

i.e.: use INSERT INTO ... SELECT' to do a query-within-insertion, which picks the latest post by Andy. I'm assuming you do also have a primary key id` that's auto-increment, which is the normal arrangement of things.

Later to get all answers to a given question, you only need to select rows whose answering attribute equals that question's id.

Alex Martelli
As I'm just getting started on this project, I can change anything I want :) I've added an "answers" column. I am not familiar enough with SQL queries to write what you propose, but logically it is something like "if user != alex, insert into (text, date_posted, user, answers) (the text, the date, the user, [most recent question primary key]. Do I need to define the most recent question as a variable via another query? Or can SQL tease that out. Thanks!
Alex Mcp
Editing my answer to provide the INSERT form I suggest.
Alex Martelli