Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 01-20-2009, 11:32 AM Row count IF
Andy Pugh's Avatar
Extreme Talker

Posts: 203
Name: Andy
Location: N.Ireland
Trades: 0
Hi there, hoping you can help me, I know it's a reasonably simple question and I probably should know the answer myself, I can't however seem to figure it out, I know I need an IF statement.

I need to basically tell the script to display - "No Results Found" in the instance that no results are indeed found...

Code as follows:

PHP Code:
<?
if ($songid == "") {
$sql="SELECT * FROM artists WHERE name LIKE '%$artist%' ORDER BY name ASC";
$result=mysql_query($sql);
$ncount=0;
while (
$myrow=mysql_fetch_array($result)) {
if (
$ncount<1)
{
$ncount=$ncount+1;
?>

<? if(isset($myrow['name'])) echo $myrow['name']; ?>
so basically if no artist is found in the directory, it displays the nothing found statement.. maybe I'm tired haha..
__________________

Please login or register to view this content. Registration is FREE
Andy Pugh is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-20-2009, 11:44 AM Re: Row count IF
anderswc's Avatar
Super Talker

Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
Trades: 0
you must be tired, that's not even valid php code

basically I think you want something like this for your while loop

PHP Code:
while ( $myrow mysql_fetch_array($result)){
$ncount++;// this counts how many rows we see
if(isset($myrow['name'])) echo $myrow['name'];
}
if(!
$ncount) echo 'No Results Found'
you could also do it without actually counting the number of rows if you wanted (though there's not much difference).

PHP Code:
while ( $myrow mysql_fetch_array($result)){
$ncount == true//at least one row
if(isset($myrow['name'])) echo $myrow['name'];
}
if(!
$ncount) echo 'No Results Found'
__________________
Will Anderson

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
anderswc is offline
Reply With Quote
View Public Profile Visit anderswc's homepage!
 
Old 01-20-2009, 01:05 PM Re: Row count IF
anderswc's Avatar
Super Talker

Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
Trades: 0
Oh yeah, you can use mysql_num_rows($result) as well.

just place that check BEFORE the while loop.
__________________
Will Anderson

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
anderswc is offline
Reply With Quote
View Public Profile Visit anderswc's homepage!
 
Old 01-20-2009, 01:07 PM Re: Row count IF
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
Use mysql_num_rows($result). No point having an isset() function call for every table row.
stoot98 is offline
Reply With Quote
View Public Profile
 
Old 01-20-2009, 02:56 PM Re: Row count IF
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
Originally Posted by stoot98 View Post
Use mysql_num_rows($result). No point having an isset() function call for every table row.
Yes there is.
An isset() call is way faster than an mysql_num_rows().
If performance is crucial and the site is slashdotted or something similar, it can make the difference between a timeout and a page.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-20-2009, 03:18 PM Re: Row count IF
anderswc's Avatar
Super Talker

Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
Trades: 0
it's not a matter of choosing between isset and mysql_num_rows though

isset doesn't really need to be there though. if there are any rows, and any of their names are "name" then it'll be there.
__________________
Will Anderson

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
anderswc is offline
Reply With Quote
View Public Profile Visit anderswc's homepage!
 
Old 01-26-2009, 10:15 AM Re: Row count IF
Andy Pugh's Avatar
Extreme Talker

Posts: 203
Name: Andy
Location: N.Ireland
Trades: 0
Hi Guys,
Thank's for your responses, really appreciate it!

I have the following code now which is operational, however, I was wondering if someone could let me know how I could loop the output so I could style it?



PHP Code:
<?

$result 
mysql_query("select * from comments WHERE tag = '$headline' and approved = 'YES' order by Name ASC");
while ( 
$myrow mysql_fetch_array($result)){
$ncount++;
if(isset(
$myrow['comment'])) echo $myrow['comment'];
if(isset(
$myrow['name'])) echo $myrow['name'];
if(isset(
$myrow['date'])) echo $myrow['date'];
}
if(!
$ncount) echo 'No Results Found'
?>
Thanks guys,
Andy
__________________

Please login or register to view this content. Registration is FREE
Andy Pugh is offline
Reply With Quote
View Public Profile
 
Old 01-26-2009, 10:48 AM Re: Row count IF
Andy Pugh's Avatar
Extreme Talker

Posts: 203
Name: Andy
Location: N.Ireland
Trades: 0
Hi!
I got it, just used the following:

PHP Code:
<?

$result 
mysql_query("select * from comments WHERE tag = '$headline' and approved = 'YES' order by Name ASC");
while ( 
$myrow mysql_fetch_array($result))

{
$ncount++;
 
$comment=$myrow["comment"];
 
$name=$myrow["name"];
 
$date=$myrow["date"];
echo 
"$name<BR>$date<BR>$comment<BR><BR>";

}
if(!
$ncount) echo 'Noone has added a comment yet, will you be the first?'
?>
Is that the cleanest way?
Thanks,
Andy
__________________

Please login or register to view this content. Registration is FREE
Andy Pugh is offline
Reply With Quote
View Public Profile
 
Old 01-26-2009, 11:09 AM Re: Row count IF
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
Quote:
Originally Posted by tripy View Post
Yes there is.
An isset() call is way faster than an mysql_num_rows().
If performance is crucial and the site is slashdotted or something similar, it can make the difference between a timeout and a page.
Sure it is faster if youre calling them once each. I doubt itll be a lot faster when you are calling isset() 3000 times Vs 1 num_rows() with a big result set. Bottom line is there is no point introducing extra code branches when one will do.
stoot98 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Row count IF
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.31095 seconds with 12 queries