Jump to content

Archived

This topic is now archived and is closed to further replies.

chuddyuk

if function

Recommended Posts

Im trying to replace data from the database with different words:
[code]
if ($collection = 'ethnicchic'){
echo "Ethnic Chic";
}
elseif ($collection = 'indiansummer'){
echo "Indian Summer";
}
elseif ($collection = 'africaninspirations'){
echo "African Inspirations";
}
elseif ($collection = 'contemporarycreations'){
echo "Contemporary Creations";
}
[/code]
i am using the above code to try and change whats in the databse to something else, in this case to put a space betweeen my letters. but it doesnt change anything. any ideas?

Share this post


Link to post
Share on other sites
== is the comparison operator. Change your code to this:
[code]
if ($collection == 'ethnicchic'){
echo "Ethnic Chic";
}
elseif ($collection == 'indiansummer'){
echo "Indian Summer";
}
elseif ($collection == 'africaninspirations'){
echo "African Inspirations";
}
elseif ($collection == 'contemporarycreations'){
echo "Contemporary Creations";
}[/code]

Share this post


Link to post
Share on other sites
You don't need to use the "elseif" in this instance, since you could write this as four individual "if" statements:
[code]<?php
if ($collection == 'ethnicchic') echo "Ethnic Chic";
if ($collection == 'indiansummer') echo "Indian Summer";
if ($collection == 'africaninspirations') echo "African Inspirations";
if ($collection == 'contemporarycreations') echo "Contemporary Creations";
?>[/code]
You can also use a "switch" statement here:
[code]<?php
switch ($collection) {
    case 'ethnicchic':
         $tmp = 'ethnic chic';
         break;
    case 'indiansummer':
         $tmp = 'indian summer';
         break;
    case 'africaninspirations':
         $tmp = 'african inspirations';
         break;
     case 'contemporarycreations':
         $tmp = 'contemporary creations';
         break;
}
echo ucwords($tmp);
?>[/code]

Just giving you other options.

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.