These are my tables:
1.topics table:
topic_id subject
1 bla bla
2 two two
2 contents table:
content_id topic_id content date
1 1 subject description 7/10/2014
2 1 reply1 7/12/2014
3 1 reply2 8/1/2014
As you can see, the topic_id
in the content table is the foreign key of the topics table. The content
column in the contents table store the description/ content of the topic (topic_id # 1, for instance
) as well as the repli(es) (content_ids # 2, #3) of the typical topic_id #1 too. So, in order to print out the subject
once, I code like this:
$printsubjectonce = FALSE; // Flag variable.
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
if (!$printsubjectonce) {
echo "{$row['subject']}
"; //subject is printed ONCE
$printsubjectonce = TRUE;
}
//print the content:
echo "{$row['content']} <br />({$row['date']}
"; //The subject description and possible repli(es) are repeatedly printed
}// End of WHILE loop.
Everything is okay. Now I would like to add a delete hyperlink below the subject content, but not below the subject line. Then, I add <a href="delete.com"> Delete </a>
below the $row['content']
. However, the output shows that the delete hyperlink(s) is/are repeatedly printed out in according with the possible number of repli(es) the subject has got. However, what I wish is to have the hyperlink printed out only once (below the subject description) in spite of how many repli(es) the subject would have.
I also tried moving the <a href=...> into the
if (!$printsubjectonce){...}', just above the code $printsubjectonce = TRUE;
, it is then logically printed out above the content section, which i didn't want to.
And I got stuck here now. Can you help me?
Try using something like this;
<?php
if (!defined('CODE_EXECUTED')) {
YOUR_CODE_HERE
define('CODE_EXECUTED', TRUE);
}
?>
You can then place the delete button where ever you like; you can use something like this to print the delete button once:
echo "{$row['content']}" . ((!defined('delButton'))?"<a href='delete.com'> Delete </a>":"") . " <br />({$row['date']}
";
define('delButton', TRUE);
That will make the delete button print out once, supposedly. test it out.
Use two if
statements. The first one is for printing the subject heading, the second one is for printing the delete link. Put the $printsubjectonce = true
assignment after the second one.
$printsubjectonce = FALSE; // Flag variable.
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
// Print subject header once
if (!$printsubjectonce) {
echo "{$row['subject']}
";
}
//print the content:
echo "{$row['content']} <br />({$row['date']}
"; //The subject description and possible repli(es) are repeatedly printed
// Print delete link once
if (!$printsubjectonce) {
echo '<a href="delete.com"> Delete </a>';
$printsubjectonce = TRUE;
}
}// End of WHILE loop.