PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.php > Problems with Elseif
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Problems with Elseif

Réponse
 
LinkBack Outils de la discussion
Vieux 18/10/2007, 15h44   #1
Alec
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Problems with Elseif

What am I doing wrong with this elseif condition?

$query = "SELECT company, priority FROM companyid_uks49179 WHERE
town='Bury St. Edmunds' AND category='sleep' AND priority IN
('1', '2', '3') ORDER BY priority";

This returns a result set of a number of companies with different
priority numbers from 1 to 3. see webpage http://www.freeweekends.co.uk/test3.php

I now want to create a table result for each one, with an 'elseif'
statement creating a different effect for each of the three different
priorities.

$result = mysql_query($query) or die ('Error in query: $query. ' .
mysql_error());

echo '<table border=1 cellpadding=10>';

while($row = mysql_fetch_object($result))

{
$priority = $row['priority'];

if ($priority == '1')

{
echo '<tr>';
echo "<td>$row->company</td>";
echo '<tr>';
echo "<td>test1</td>";
echo '<tr>';
}

elseif ($priority == '2')

{
echo '<tr>';
echo "<td>$row->company</td>";
echo '<tr>';
echo "<td>test2</td>";
echo '<tr>';
}

else

{
echo '<tr>';
echo "<td>$row->company</td>";
echo '<tr>';
echo "<td>test3</td>";
echo '<tr>';
}

}
echo '</table>';

This returns all the results as if they were not priority 1 or 2. See
http://www.freeweekends.co.uk/test2.php

Beginners question I know, but what am I doing wrong???

Many thanks

Alec

  Réponse avec citation
Vieux 18/10/2007, 17h33   #2
J.O. Aho
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problems with Elseif

Alec wrote:
> What am I doing wrong with this elseif condition?
>
> $query = "SELECT company, priority FROM companyid_uks49179 WHERE
> town='Bury St. Edmunds' AND category='sleep' AND priority IN
> ('1', '2', '3') ORDER BY priority";
>
> This returns a result set of a number of companies with different
> priority numbers from 1 to 3. see webpage http://www.freeweekends.co.uk/test3.php
>
> I now want to create a table result for each one, with an 'elseif'
> statement creating a different effect for each of the three different
> priorities.


I'm that much into usage of else if when I have this kind of options, I
suggest you try out a switch.
> $result = mysql_query($query) or die ('Error in query: $query. ' .
> mysql_error());
>
> echo '<table border=1 cellpadding=10>';
>
> while($row = mysql_fetch_object($result)) {

//No values are in an array['priority'], they are in an object->priority;
switch($row->priority) {
case '1':
$type='test1';
break;
case '2':
$type='test2';
break;
case '3':
$type='test3';
break;
default:
$type='unknown';
break;
}
echo "<tr>\n<td>{$row->company}</td>\n</tr>\n";
echo "<tr>\n<td>{$type}</td>\n</td>\n";
>
> }
> echo '</table>';


You may have to take a better look at your indentation and your html validity.
Don't duplicate code when you can have it once, this way you can make your
code shorter, functions will be a good thing to use if you have same code that
you need to use in different places.

--

//Aho
  Réponse avec citation
Vieux 18/10/2007, 17h50   #3
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problems with Elseif

Alec wrote:
> What am I doing wrong with this elseif condition?
>
> $query = "SELECT company, priority FROM companyid_uks49179 WHERE
> town='Bury St. Edmunds' AND category='sleep' AND priority IN
> ('1', '2', '3') ORDER BY priority";
>
> This returns a result set of a number of companies with different
> priority numbers from 1 to 3. see webpage http://www.freeweekends.co.uk/test3.php
>
> I now want to create a table result for each one, with an 'elseif'
> statement creating a different effect for each of the three different
> priorities.
>
> $result = mysql_query($query) or die ('Error in query: $query. ' .
> mysql_error());
>
> echo '<table border=1 cellpadding=10>';
>
> while($row = mysql_fetch_object($result))
>
> {
> $priority = $row['priority'];
>
> if ($priority == '1')
>
> {
> echo '<tr>';
> echo "<td>$row->company</td>";
> echo '<tr>';
> echo "<td>test1</td>";
> echo '<tr>';
> }
>
> elseif ($priority == '2')
>
> {
> echo '<tr>';
> echo "<td>$row->company</td>";
> echo '<tr>';
> echo "<td>test2</td>";
> echo '<tr>';
> }
>
> else
>
> {
> echo '<tr>';
> echo "<td>$row->company</td>";
> echo '<tr>';
> echo "<td>test3</td>";
> echo '<tr>';
> }
>
> }
> echo '</table>';
>
> This returns all the results as if they were not priority 1 or 2. See
> http://www.freeweekends.co.uk/test2.php
>
> Beginners question I know, but what am I doing wrong???
>
> Many thanks
>
> Alec
>
>



Maybe it should be

if ($priority == 1) ...

And J.O.'s suggestion to use switch is good.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 18/10/2007, 17h53   #4
J.O. Aho
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problems with Elseif

Jerry Stuckle wrote:
> Alec wrote:
>> What am I doing wrong with this elseif condition?
>>
>> $query = "SELECT company, priority FROM companyid_uks49179 WHERE
>> town='Bury St. Edmunds' AND category='sleep' AND priority IN
>> ('1', '2', '3') ORDER BY priority";
>>
>> This returns a result set of a number of companies with different
>> priority numbers from 1 to 3. see webpage
>> http://www.freeweekends.co.uk/test3.php
>>
>> I now want to create a table result for each one, with an 'elseif'
>> statement creating a different effect for each of the three different
>> priorities.
>>
>> $result = mysql_query($query) or die ('Error in query: $query. ' .
>> mysql_error());
>>
>> echo '<table border=1 cellpadding=10>';
>>
>> while($row = mysql_fetch_object($result))
>>
>> {
>> $priority = $row['priority'];


> Maybe it should be
>
> if ($priority == 1) ...
>
> And J.O.'s suggestion to use switch is good.
>

The $row is a object, and he tries to use it as an array, so $priority is
nothing/null, which mean it never can be 1,2 and therefore always will fall in
the last else-statement.

--

//Aho
  Réponse avec citation
Vieux 18/10/2007, 19h10   #5
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problems with Elseif

J.O. Aho wrote:
> Jerry Stuckle wrote:
>> Alec wrote:
>>> What am I doing wrong with this elseif condition?
>>>
>>> $query = "SELECT company, priority FROM companyid_uks49179 WHERE
>>> town='Bury St. Edmunds' AND category='sleep' AND priority IN
>>> ('1', '2', '3') ORDER BY priority";
>>>
>>> This returns a result set of a number of companies with different
>>> priority numbers from 1 to 3. see webpage
>>> http://www.freeweekends.co.uk/test3.php
>>>
>>> I now want to create a table result for each one, with an 'elseif'
>>> statement creating a different effect for each of the three different
>>> priorities.
>>>
>>> $result = mysql_query($query) or die ('Error in query: $query. ' .
>>> mysql_error());
>>>
>>> echo '<table border=1 cellpadding=10>';
>>>
>>> while($row = mysql_fetch_object($result))
>>>
>>> {
>>> $priority = $row['priority'];

>
>> Maybe it should be
>>
>> if ($priority == 1) ...
>>
>> And J.O.'s suggestion to use switch is good.
>>

> The $row is a object, and he tries to use it as an array, so $priority is
> nothing/null, which mean it never can be 1,2 and therefore always will fall in
> the last else-statement.
>


Ah, I missed that one. Good catch! I just always use
mysql_fetch_array(). :-)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 18/10/2007, 19h17   #6
J.O. Aho
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problems with Elseif

Jerry Stuckle wrote:
> J.O. Aho wrote:
>> Jerry Stuckle wrote:
>>> Alec wrote:
>>>> What am I doing wrong with this elseif condition?
>>>>
>>>> $query = "SELECT company, priority FROM companyid_uks49179 WHERE
>>>> town='Bury St. Edmunds' AND category='sleep' AND priority IN
>>>> ('1', '2', '3') ORDER BY priority";
>>>>
>>>> This returns a result set of a number of companies with different
>>>> priority numbers from 1 to 3. see webpage
>>>> http://www.freeweekends.co.uk/test3.php
>>>>
>>>> I now want to create a table result for each one, with an 'elseif'
>>>> statement creating a different effect for each of the three different
>>>> priorities.
>>>>
>>>> $result = mysql_query($query) or die ('Error in query: $query. ' .
>>>> mysql_error());
>>>>
>>>> echo '<table border=1 cellpadding=10>';
>>>>
>>>> while($row = mysql_fetch_object($result))
>>>>
>>>> {
>>>> $priority = $row['priority'];

>>
>>> Maybe it should be
>>>
>>> if ($priority == 1) ...
>>>
>>> And J.O.'s suggestion to use switch is good.
>>>

>> The $row is a object, and he tries to use it as an array, so
>> $priority is
>> nothing/null, which mean it never can be 1,2 and therefore always will
>> fall in
>> the last else-statement.
>>

>
> Ah, I missed that one. Good catch! I just always use
> mysql_fetch_array(). :-)


Thats the most common one I use, but nowadays I been more using mysqli, but
when writing soap server stuff, I have run into trouble of using mysqli and
have in those use mysql instead. So I compile always php with both mysql and
mysqli support, at least until I have rewritten all old scripts and the soap
trouble is solved somehow.


--

//Aho
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h35.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16790 seconds with 14 queries