Be a Supporter!

mysql while loop - NOT?

  • 392 Views
  • 12 Replies
New Topic Respond to this Topic
yhar
yhar
  • Member since: Apr. 2, 2008
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
mysql while loop - NOT? 2008-06-06 11:14:41 Reply

<?php
$result= mysql_query("SELECT * FROM pages WHERE `site`='$domain'");
while($row = mysql_fetch_array($result)) {
echo $row['title'];
}
?>

(Codes more complex than that (That's just the part needed).

I'm building my own miniCMS and I've got a navigation problem, i want to be able to echo every sites page EXCEPT home, that will be done manually. Now my problem is, my while loop echos every page, including Home.

How can i tell it NOT to include "Home" in the while loop? Is there some sort of "NOT" command? I tried searching and it's come up with nothing. So like;

$result= mysql_query("SELECT * FROM pages WHERE `site`='$domain' NOT WHERE page='Home'");

Thanks,
Sam.
x


THIS IS CITRICSQUID POSTING

elbekko
elbekko
  • Member since: Jul. 23, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to mysql while loop - NOT? 2008-06-06 11:17:29 Reply

$result= mysql_query("SELECT * FROM pages WHERE `site`='$domain' WHERE page != 'Home'");

"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature
yhar
yhar
  • Member since: Apr. 2, 2008
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to mysql while loop - NOT? 2008-06-06 11:23:24 Reply

At 6/6/08 11:17 AM, elbekko wrote: $result= mysql_query("SELECT * FROM pages WHERE `site`='$domain' WHERE page != 'Home'");

Thanks, but i didn't explain myself properly.
The function is supposed to echo every listed title in the DB, so like;

Home
About
Cats
Penis

However the code above goes too far, it selects from the page itself, do you understand what i mean?

What i need is something like, "Select from pages where site='$site' BUT IGNORE 'Home'"

make sense?

tyia :)

THIS IS CITRICSQUID POSTING

yhar
yhar
  • Member since: Apr. 2, 2008
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to mysql while loop - NOT? 2008-06-06 11:56:49 Reply

I looked around and can't find anything that will work, i have a work around though :)
So yeah, solved :)


THIS IS CITRICSQUID POSTING

Jon-86
Jon-86
  • Member since: Jan. 30, 2007
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to mysql while loop - NOT? 2008-06-06 12:36:42 Reply

if($row['title'] != "home")
echo $row['title'];

Mabey thats what you want? Instead of changing the SQL


PHP Main :: C++ Main :: Java Main :: Vorsprung durch Technik
irc.freenode.net #ngprogramming

BBS Signature
yhar
yhar
  • Member since: Apr. 2, 2008
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to mysql while loop - NOT? 2008-06-06 12:41:28 Reply

At 6/6/08 12:36 PM, Jon-86 wrote: if($row['title'] != "home")
echo $row['title'];

Mabey thats what you want? Instead of changing the SQL
$resulttt = mysql_query("SELECT site, title, content FROM pages WHERE site='$domain'");  
while ($rowww = mysql_fetch_assoc($resulttt)) { 	
if ($rowww['title'] == 'Home') {
continue; 	
}
echo "<li><a href=\"";
echo $rowww['title'];
echo "\">";
echo $rowww['title'];
echo "</a></li>";
}

Thats what i settled with in the end, it works perfectly :)


THIS IS CITRICSQUID POSTING

DannyIsOnFire
DannyIsOnFire
  • Member since: Apr. 14, 2005
  • Offline.
Forum Stats
Member
Level 21
Movie Buff
Response to mysql while loop - NOT? 2008-06-06 12:46:39 Reply

At 6/6/08 11:56 AM, yhar wrote: I have a work around though :)

Ah, work-arounds. A programmers best friend :)


|| Portfolio || Facebook || Twitter ||

BBS Signature
yhar
yhar
  • Member since: Apr. 2, 2008
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to mysql while loop - NOT? 2008-06-06 12:57:50 Reply

At 6/6/08 12:46 PM, DannyIsOnFire wrote:
At 6/6/08 11:56 AM, yhar wrote: I have a work around though :)
Ah, work-arounds. A programmers best friend :)

*porn


THIS IS CITRICSQUID POSTING

BoneIdol
BoneIdol
  • Member since: Aug. 14, 2006
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to mysql while loop - NOT? 2008-06-06 13:19:58 Reply

<?php
$result= mysql_query("SELECT * FROM pages WHERE `site`='$domain' AND page IS NOT 'home'");
while($row = mysql_fetch_array($result)) {
echo $row['title'];
}
?>

7 replies and no one suggested that? It's probably in the 1st or 2nd chapter of every single SQL book and can be effortlessly found via search engine.


Sufficiently advanced incompetence is indistinguishable from malice.

BoneIdol
BoneIdol
  • Member since: Aug. 14, 2006
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to mysql while loop - NOT? 2008-06-06 14:08:06 Reply

Whoops elbeko didnt see the !=, AND I used the sql you'd use in the select bit rather than the where bit. *eats humble pie*

While we are on this issue, why not just not select the page your on? Or would you prefer the title/page value to be null if it's the current page? If that's the case, well, let's use a case statement (yes, yes stop groaning now).

SELECT title, stuff, etc, CASE page
    WHEN 'home' THEN ''
    ELSE page
  END
FROM pages
WHERE site='website';

Might need a spot of debugging.


Sufficiently advanced incompetence is indistinguishable from malice.

yhar
yhar
  • Member since: Apr. 2, 2008
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to mysql while loop - NOT? 2008-06-06 14:18:01 Reply

At 6/6/08 02:08 PM, BoneIdol wrote: While we are on this issue, why not just not select the page your on? Or would you prefer the title/page value to be null if it's the current page? If that's the case, well, let's use a case statement (yes, yes stop groaning now).

I love you.
You just gave me an awesome idea for it :)
Thanks!


THIS IS CITRICSQUID POSTING

BoneIdol
BoneIdol
  • Member since: Aug. 14, 2006
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to mysql while loop - NOT? 2008-06-06 14:26:26 Reply

Word of warning, before I sod off to the pub to celebrate that start of my holiday. I THINK case statements are mysql 5 only, so it might not work in mysql 4.

It works on my setup at work anyway, but my SQL goes through code igniter. You may need to use the mysqli or pdo libraries.


Sufficiently advanced incompetence is indistinguishable from malice.

plasmaz
plasmaz
  • Member since: Feb. 13, 2007
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to mysql while loop - NOT? 2008-06-06 19:32:03 Reply

Since you are writing a CMS you should look into getting away from the standard mysql items. Check out MVC (Zend, Code Igniter), or the PEAR library (http://pear.php.net).

PEAR is easy to setup (comes with php) and has a nice package called DB_DataObject.

$navigation = DB_DataObject::factory("pages");
$navigation -> site = $domain;
$navigation -> whereAdd( "lower(page) <> 'home'" );
$navigation ->find();
while( $navigation -> fetch() )
  print( $navigation -> title .' :: ' );

Just something to keep in mind, as the PEAR library is very clean and is becoming more OOP just like PHP.


- Michael McMullen
Wii Messages: http://www.wii-messages.com

BBS Signature