Forum Moderators: coopster

Message Too Old, No Replies

Array else if or statement

Where id number does not exist in database

         

oceanwave

1:16 am on Oct 29, 2007 (gmt 0)

10+ Year Member



If the array named 'type' in the database is equal to 'N;' (when no checkbox information is selected from a form, the value 'N;' is inserted into the 'type' field), this code posts 'Information Unavailable' on a php page.

<?php
if($hl['type']!= 'N;'){
$myarray = unserialize($hl['type']);
$mylist = implode('<br>',$myarray);
echo $mylist;
} else {
echo 'Information Unavailable';
}
?>

This works well. Since this is posted on a php page that looks like: /page.php?id=9 , if id=9 does not exist in the database, the template page shows "Warning: implode() [function.implode]: Bad arguments. in..." where the information for the field 'type' would be inserted. This is only true if the database entry does not exist.

Though this is a small issue since I am talking about non-existant id numbers, I am still curious how to make the 'type' field also echo 'Information Unavailable' if the id entry does not exist. I tried elseif statements, I tried ¦¦ (or) statements, but I am not able to get this to work.

What is the correct way to do this?

Habtom

4:24 am on Oct 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This might do the job:

if($hl['type']!= 'N;' && $hl['id']!= ''){

Habtom

FiRe

1:44 pm on Oct 29, 2007 (gmt 0)

10+ Year Member



Try this...

if($hl['type']!= 'N;'){
$myarray = unserialize($hl['type']);
if ($mylist = @implode('<br>',$myarray)) {
echo $mylist;
} else {
echo 'Information Unavailable';
}
}
else {
echo 'Information Unavailable';
}

oceanwave

2:31 pm on Oct 29, 2007 (gmt 0)

10+ Year Member



Hi Habtom and FiRe,

Habtom, I tried your example and it works perfectly but I don't understand why. When I first looked at it, I thought the && would mean that both the 'type' field would have to be 'N;' AND the id number could not exist. I thought this would mess up the pages where the id EXISTED in the database with a type field of 'N;' (that I would no longer get the message 'Information Unavailable' for these pages). However that was not the case, everything works fine. Could you please straighten out my reasoning regarding the && statement? I thought I needed some kind of "or" statement, where type=N; OR id did not exist. I never even considered an && statement.

FiRe, I haven't tried your suggestion yet, but will also try it to see how it works.

Thank you both!

Habtom

5:03 am on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it working.

if($hl['type']!= 'N;' && $hl['id']!= ''){

The '&&' in the above line means 'and' and the two conditions should be met for the rest of the code to be executed in the if statement.

This part of the code!= '' ensures that the id is not actually empty, which is not the case you wanted under which the if condition to be executed.

Habtom

oceanwave

3:41 pm on Oct 31, 2007 (gmt 0)

10+ Year Member



Hi Habtom,

Thanks for the explanation. The part I didn't understand is, I thought that if the id # did not exist in the database, the id # was empty. So that pages that didn't exist would have empty "type" fields and empty "id" fields. I thought pages that existed could have a "type" field with a value of "N;" and a value in the "id" field, hense both fields had values. That is why I thought your statement could not work in both of those conditions, but apparently I was wrong.

Thanks again.