Jump to content


Photo

While Within A While ?

while announcements announce

  • Please log in to reply
5 replies to this topic

#1 Clinton

Clinton

    Advanced Member

  • Members
  • PipPipPip
  • 337 posts

Posted 26 November 2012 - 06:28 PM

First, the idea: I want to post announcements for all users and then allow them to "clear" them off of their announcement list.

I have two tables.

Table 1 (announce) holds announcements. It has the fields of id, date, and info.

Table 2 (ackannounce) holds the fact that a user has "cleared/acknowledged" an announcement and contains the fields aid (announcement ID) and uid (user ID).

Essentially I want the code to check the table 'ackannounce' and then display whatever announcements have NOT been "cleared/acknowledged" by the user.

The code below, however, is not accomplishing that (all the variables are fine and populate appropriately).

Any help in demistifying this for me would be greatly appreciated.

<?
$sql="SELECT * FROM ackannounce WHERE uid='$uid'";
$result=mysql_query($sql); ?>
<p>
<center>
<font color="green">
<table width="85%" border="3" cellspacing="0" cellpadding="0">
<?
while($row = mysql_fetch_array($result))
{
$aid = $row['aid'];
$sql2="SELECT * FROM announce WHERE NOT id = '$aid'";
$result2=mysql_query($sql2);
while($row = mysql_fetch_array($result2))
{
$id = $row['id'];
$info = $row['info'];
?>
<tr><td width="20%">Date: <? echo date("m-d-Y",strtotime($row['date']));?> </td><td><br><? echo $info; ?><br>&nbsp;<br><a href="ackannounce.php?aid=<?echo $id;?>&uid=<?echo $uid; ?>">Acknowledge/Clear</a><br>&nbsp;</td></tr>
<? }} ?>
</table>
</font>
</center>
</p>


#2 Muddy_Funster

Muddy_Funster

    Advanced Member

  • Members
  • PipPipPip
  • 2,993 posts

Posted 26 November 2012 - 06:49 PM

Don't run queries within loops a single query is all you need
$sql = "SELECT id, date, info FROM announce WHERE id not in (SELECT aid from ackannounce WHERE uid = '$uid')"

A simple password hash :

function makePass($word=''){
  $dbSalt = '$2a$07$'.substr(hash('whirlpool',$word),0,22);
  $dbPass = crypt($word, $dbSalt);
 return substr($dbPass,12);
}



My SQL/PHP Blog

#3 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 26 November 2012 - 11:48 PM

Muddy, that's not a single query.

My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#4 Muddy_Funster

Muddy_Funster

    Advanced Member

  • Members
  • PipPipPip
  • 2,993 posts

Posted 27 November 2012 - 03:33 AM

Muddy, that's not a single query.

now your just being pedantic :P I only ment that a single query string should be run against the database, yeas it's two queries, or rather one query and a sub query, but I know you know that I know you know what I ment :P
A simple password hash :

function makePass($word=''){
  $dbSalt = '$2a$07$'.substr(hash('whirlpool',$word),0,22);
  $dbPass = crypt($word, $dbSalt);
 return substr($dbPass,12);
}



My SQL/PHP Blog

#5 Clinton

Clinton

    Advanced Member

  • Members
  • PipPipPip
  • 337 posts

Posted 27 November 2012 - 10:40 AM

Man, that's a lot of 'knows'! :-)

Awesome, it worked. Thanks, Muddy. :happy-04:

#6 Muddy_Funster

Muddy_Funster

    Advanced Member

  • Members
  • PipPipPip
  • 2,993 posts

Posted 27 November 2012 - 10:58 AM

No problem, glad to help.
A simple password hash :

function makePass($word=''){
  $dbSalt = '$2a$07$'.substr(hash('whirlpool',$word),0,22);
  $dbPass = crypt($word, $dbSalt);
 return substr($dbPass,12);
}



My SQL/PHP Blog




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com