/    Sign up×
Community /Pin to ProfileBookmark

"if else" inside of different "if else"

I’d like to ask is this correct to put “if/else” statement inside other “if/else” statement? For example:

[code=php]
<?php

$count=mysql_num_rows($result);
if($_POST[‘Submit’])
{
foreach($_POST[‘id’] as $id)
{

}
if($result1){

}
}
else
{
?>

<?php
while($rows=mysql_fetch_array($result))
{
?>

<?php
}
?>

<?php
// SHOULD IT BE HERE?
if($count == 0) {
echo “Table is empty”;
}
else {
echo “Table loaded”;
}
?>

<?php
}

mysql_close();
?>

[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@JonaApr 21.2010 — [font=arial]You can nest if/else statements within each other. There's nothing wrong with that.

The example you posted might not produce an error, but I don't think it will produce the intended effect. Your loop will attempt to execute even when zero rows exist. You should check whether or not rows exist prior to beginning your loop. Like so:[/font]

[code=php]
<?php

$count=mysql_num_rows($result);

if($_POST['Submit'] && $count > 0)
{
foreach($_POST['id'] as $id)
{
...
}
if($result1){
while($rows=mysql_fetch_array($result))
{
...
}
mysql_close();
}
} else {
if($count == 0){
echo 'Table is empty.';
} else {
echo 'Table is not empty, but the form was not submitted.';
}
}
?>
[/code]
Copy linkTweet thisAlerts:
@HelleshternauthorApr 21.2010 — I see. Actually I have a table which is made just from some exact data from database, like:

[code=php]$sql="SELECT * FROM $table_name WHERE prefix = 1";[/code]

When there's no rows with "prefix = 1" than it shows empty table. The thing I need is to message shows in table only when there's no rows to display.
Copy linkTweet thisAlerts:
@JonaApr 21.2010 — I see. Actually I have a table which is made just from some exact data from database, like:

[code=php]$sql="SELECT * FROM $table_name WHERE prefix = 1";[/code]

When there's no rows with "prefix = 1" than it shows empty table. The thing I need is to message shows in table only when there's no rows to display.[/QUOTE]


[font=arial]So you can use an [/font][font=courier new]if[/font][font=arial] statement to ascertain whether your MySQL result has any rows, and adjust your output accordingly. The code I posted above does that. Here's a simplified version:[/font]

[code=php]
<?php

$result = mysql_query("SELECT * FROM $table_name WHERE prefix = 1");
$count = mysql_num_rows($result);

if ($count > 0){
echo 'This table has data.';
// put all your code for outputting the table, like your foreach loop, here
} else {
echo 'No data in this table.';
}

?>
[/code]
Copy linkTweet thisAlerts:
@JonaApr 21.2010 — I see. Actually I have a table which is made just from some exact data from database, like:

[code=php]$sql="SELECT * FROM $table_name WHERE prefix = 1";[/code]

When there's no rows with "prefix = 1" than it shows empty table. The thing I need is to message shows in table only when there's no rows to display.[/QUOTE]


[font=arial]So you can use an [/font][font=courier new]if[/font][font=arial] statement to ascertain whether your MySQL result has any rows, and adjust your output accordingly. The code I posted above does that. Here's a simplified version:[/font]

[code=php]
<?php

$result = mysql_query("SELECT * FROM $table_name WHERE prefix = 1");
$count = mysql_num_rows($result);

if ($count > 0){
echo 'This table has data.';
// put all your code for outputting the table, like your foreach loop, here
} else {
echo 'No data in this table.';
}

?>
[/code]
Copy linkTweet thisAlerts:
@HelleshternauthorApr 24.2010 — Thank you very much.
×

Success!

Help @Helleshtern spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...