0

I have a a table named books containg fields as

book_id | title | author | summary

With the code below I query db and display list of book titles and authors. After listing, whenever a title link of a book clicked I want to display the summary of that book without querying the db again... How can I accomplish this without re-querying db and just using the data stored in $books_query_result?

$books_query="SELECT * FROM books";
$books_query_result=mysql_query($books_query) or die("Could not get list".mysql_error());

echo "<table><tr>";
echo "<td>";


  while($list=mysql_fetch_array($books_query_result)){
    echo "<A href=\"" .$list['book_id'] ."\"> ". $list['title']. "</a>";
    echo $list['author']."<br>";
  }


# Now I want to reset and re-use mysql_fetch_array

  mysql_data_seek($books_query_result, 0); 
  $summary=mysql_fetch_array($books_query_result);


  echo "</td>";
  echo "<td>";

#display summary here


  echo "<td>";
  echo "</tr></table>";
2
  • 3
    Don't do this, just store your fetched data in an array which you can reuse lter. Commented May 11, 2012 at 6:45
  • 1
    The second time you have not the same resultset as before: books vs. $selectcompany_query_result Commented May 11, 2012 at 6:49

2 Answers 2

1

Print all the data like it should be visible and the summaries into div's with `style="display:none". Then on the link add the call to the function which is for toggling visibility: http://www.randomsnippets.com/2008/02/12/how-to-hide-and-show-your-div/. Make sure you also add that function into your javascript. This way your data is always there and you won't need to reload it.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are looking for resetting and iterating $books_query_result again, than you should use mysql_data_seek($books_query_result, 0); instead mysql_data_seek($selectcompany_query_result, 0).

Just prepare the array inside while loop and use it anywhere later

while($list=mysql_fetch_array($books_query_result)){
    //echo "<A href=\"" .$list['book_id'] ."\"> ". $list['title']. "</a>";
    //echo $list['author']."<br>";

    // prepare array here
    $resultArray[] = $list;
}

Now use the $resultArray.

3 Comments

Oopps, it was typing mistake. I edited the question. Thank you.
After that do we need javascript to display summary when a title clicked?
@mustafa, yes you can than on use the array as you wish, same as we are using other php arrays multiple times

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.