Jump to content

Recommended Posts

I've been trying to figure out how to delete the last comma of a list of database entries for a while now. For example

http://192.168.1.3/potg/?get=books&author=Ed%20Rosenthal

it lists all the books written by Ed Rosenthal. Each book is separated by a comma but obviously there shouldn't be a comma after the last book listed. Heres the code I'm using.

$query = "SELECT * FROM potg_books";

$resultb = mysql_query($query);


$numb = mysql_num_rows($resultb);



$ib=0;

while ($ib < $numb) {


$book_id = mysql_result($resultb,$ib,"id");

$book_title = mysql_result($resultb,$ib,"book_title");
$pub_year = mysql_result($resultb,$ib,"pub_year");

if (preg_match("/_ $book_id _/", $books)) { echo " <a href=\"?get=books&id=$book_id\">$book_title ($pub_year)</a>, "; }


++$ib;

}

Heres the DB entry for Ed Rosenthal


INSERT INTO `potg_authors` (`id`, `author_name`, `info`, `books`) VALUES
(14, 'Ed Rosenthal', 'Ed Rosenthal (born Bronx, New York, 1944) is a California horticulturist, author, publisher, and Cannabis grower known for his advocacy for the legalization of marijuana (cannabis as a drug) use. He served as a columnist for High Times Magazine during the 80''s and 90''s.[1] He was arrested in 2002 for cultivation of cannabis by federal authorities, who do not recognize the authority of states to regulate the use of medical marijuana. He was convicted in Federal Court, but the conviction was overturned on appeal. Rosenthal was subsequently convicted again, but was not re-sentenced, since his original sentence had been completed. Rosenthal briefly attended Youngstown State University in Youngstown, Ohio.<br><br>\r\n\r\nRosenthal has been active in promoting and developing policies of civil regulation for medicinal marijuana. With the passage of California''s pioneering Proposition 215 in 1996, which authorizes medicinal use of marijuana, he worked with the state and local governments to implement the delivery of pharmaceutical grade cannabis to patients with a doctor''s recommendation to use marijuana.', '_ 13 __ 14 __ 15 _');

What I've done is added a field called books to my authors DB table and when I wanna list all the books written by each author I just do MySQL query and list every entry in the books table that coincides with that books field. It works well but I just can't figure out how to delete the last comma of the list. Anyone know how to do this?

usually what i do instead of printing each entry with a comma at the end, i put them in an array then implode them...like so:

 

<?php
$query = "SELECT * FROM potg_books";

$resultb = mysql_query($query);


$numb = mysql_num_rows($resultb);


$list = array();
for($ib=0;$ib < $numb$ib++) {
   $book_id = mysql_result($resultb,$ib,"id");
   $book_title = mysql_result($resultb,$ib,"book_title");
   $pub_year = mysql_result($resultb,$ib,"pub_year");
   if (preg_match("/_ $book_id _/", $books)) {
      $list[] = "<a href=\"?get=books&id=$book_id\">$book_title ($pub_year)</a>";
   }
}
echo implode(", ",$list);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.