Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

While... Else?? 1

Status
Not open for further replies.

d0nny

IS-IT--Management
Dec 18, 2005
278
GB

Now, I know this doesn't exist in PHP but I'm struggling to think of a way of doing it.
I have read that the easiest way of achieving this is by nesting a While loop in an IF ELSE statement??

Here's my code
Code:
$query = "select *,date_format(dateadded , '%W %e %M') as fDate from aboutcontent where page='press' and datestart<=CURDATE()";
$result = mysql_query($query);
if ( $pagerow >= 1 ) {
   while ($row = mysql_fetch_assoc($result)) {
   $row['dateadded'] =& $row['fDate'];
   echo '<div><strong>'.$row[title].'</strong></div>';
   echo '<div style="font-size:12px;">'.$row[dateadded].'</div>';
   echo '<div style="font-size:15px;">'.$row[content].'</div>';
   echo '<br />';
   }
   } else {
      echo 'You have no stories to display.';
   }

But this seems to always show the ELSE echo??
What is the best way of achieving this?
 
Hi

d0nny said:
But this seems to always show the ELSE echo??
And for which values of $pagerow should not happen that ? By the way, where it gets its value ? If nowhere, then add this :
PHP:
[navy]$query[/navy] [teal]=[/teal] [green][i]"select *,date_format(dateadded , '%W %e %M') as fDate from aboutcontent where page='press' and datestart<=CURDATE()"[/i][/green][teal];[/teal]
[navy]$result[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]mysql_query[/color][teal]([/teal][navy]$query[/navy][teal]);[/teal]
[highlight][navy]$pagerow[/navy][teal]=[/teal][url=http://php.net/mysql_num_rows/][COLOR=darkgoldenrod]mysql_num_rows[/color][/url][teal]([/teal][navy]$result[/navy][teal]);[/teal][/highlight]
[b]if[/b] [teal]([/teal] [navy]$pagerow[/navy] [teal]>=[/teal] [purple]1[/purple] [teal])[/teal] [teal]{[/teal]
  [b]while[/b] [teal]([/teal][navy]$row[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]mysql_fetch_assoc[/color][teal]([/teal][navy]$result[/navy][teal]))[/teal] [teal]{[/teal]
    [navy]$row[/navy][teal][[/teal][green][i]'dateadded'[/i][/green][teal]][/teal] [teal]=&[/teal] [navy]$row[/navy][teal][[/teal][green][i]'fDate'[/i][/green][teal]];[/teal]
    [b]echo[/b] [green][i]'<div><strong>'[/i][/green][teal].[/teal][navy]$row[/navy][teal][[/teal]title[teal]].[/teal][green][i]'</strong></div>'[/i][/green][teal];[/teal]
    [b]echo[/b] [green][i]'<div style="font-size:12px;">'[/i][/green][teal].[/teal][navy]$row[/navy][teal][[/teal]dateadded[teal]].[/teal][green][i]'</div>'[/i][/green][teal];[/teal]
    [b]echo[/b] [green][i]'<div style="font-size:15px;">'[/i][/green][teal].[/teal][navy]$row[/navy][teal][[/teal]content[teal]].[/teal][green][i]'</div>'[/i][/green][teal];[/teal]
    [b]echo[/b] [green][i]'<br />'[/i][/green][teal];[/teal]
  [teal]}[/teal]
[teal]}[/teal] [b]else[/b] [teal]{[/teal]
  [b]echo[/b] [green][i]'You have no stories to display.'[/i][/green][teal];[/teal]
[teal]}[/teal]

Feherke.
 

Sorry, that is incorrect.
Here is my actual code:
Code:
$query = "select *,date_format(dateadded , '%W %e %M') as fDate from aboutcontent where page='press' and datestart<=CURDATE()";
$result = mysql_query($query);
[red]if ( $row >= 1 ) {[/red]
   while ($row = mysql_fetch_assoc($result)) {
   $row['dateadded'] =& $row['fDate'];
   echo '<div><strong>'.$row[title].'</strong></div>';
   echo '<div style="font-size:12px;">'.$row[dateadded].'</div>';
   echo '<div style="font-size:15px;">'.$row[content].'</div>';
   echo '<br />';
   }
   } else {
      echo 'You have no stories to display.';
   }
 
Hi

Ok, Then I also rephrase :

And for which values of $row should not happen that ? By the way, where it gets its value before that test ?

My suggested solution remains valid : use [tt]mysql_num_rows()[/tt] :
Code:
[b]if[/b] [teal]([/teal] [COLOR=darkgoldenrod]mysql_num_rows[/color][teal]([/teal][navy]$result[/navy][teal])[/teal] [teal])[/teal] [teal]{[/teal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top