Jump to content

[SOLVED] php and if statement


MDanz

Recommended Posts

on mysql i have this field known as type, which i save as a varchar 1 character.  And the 4 selections are I, V, F and M standing for Image, video, file and music.

 

now the dilemna is If statements.  I have the graphics hyperlink represent these 4 selections e.g. www.something.com/Video.jpg

 

how do i use an if statement to say..

 

If type V chosen then attach hyperlink (www.something.com/Video.jpg)

 

 

srry if it sounds complicated

Link to comment
https://forums.phpfreaks.com/topic/169037-solved-php-and-if-statement/
Share on other sites

<?php 

$type = $row['type']; // or whatever

switch ($type) {
    case 'I':
        echo "EEMAYGE";
        break;
    case 'V':
        echo "VIDYA";
        break;
    case 'F':
        echo "FIYLE";
        break;
    case 'M':
        echo "MUSAK";
        break;
    default:
       echo "OON KNEWN TYPE!";
}

 

or you could use several if statement

 

<?php 

$type = $row['type']; // or whatever


if ($type == 'I') {
        echo "EEMAYGE";
} elseif ($type == 'V') {
        echo "VIDYA";
} elseif ($type == 'F') {
        echo "FIYLE";
} elseif ($type == 'M') {
        echo "MUSAK";
} else {
       echo "OON KNEWN TYPE!";
}

  $hyperlink = $_POST['hyperlink'];

$currency = $_POST['currency'];

$name = $_POST['name'];

$image = $_POST['image'];

$info = $_POST['info'];

$keywords = $_POST['keywords'];

$type = $_POST['type'];

 

      // Create the query and insert

      // into our database.

      $query = "INSERT INTO Upload ";

      $query .= "(`image`, `hyperlink`,`currency`,`name`,`info`,`keywords`,`type`) VALUES ('$image','$hyperlink','$currency','$name','$info','$keywords','$type')";

 

      $results = mysql_query($query, $link);

 

heres the code for when i upload it

 

 

how do i add an if statement to say..

 

If Type = V then http://www.something.com/Video.jpg

 

and it inserts that link into the database.. I've already changed the field type in the database varchar to 200 characters

<?php 

$type = $row['type']; // or whatever

switch ($type) {
    case 'I':
        echo "EEMAYGE";
        break;
    case 'V':
        echo "VIDYA";
        break;
    case 'F':
        echo "FIYLE";
        break;
    case 'M':
        echo "MUSAK";
        break;
    default:
       echo "OON KNEWN TYPE!";
}

 

thx i'll try this out

ok, but you won't need this line

 

$type = $row['type']; // or whatever

 

because you'll already have defined type as...

 

$type = $_POST['type'];

 

also, if you're testing it...

 

instead of

 

$results = mysql_query($query, $link);

 

just use

 

echo $query;

 

to see what SQL query it would do... just saves you from removing loads of useless results (unless you're going to flush the table before you use it properly?)

 

---------

 

if you're doing what I tihnk you're doing...

 

if it's a video display a picture of a camcorder

if it's a picture a picture of a camera

a file picture of a file

image, picture of an image...

 

just leave it as I F V M in the database then use the code I sent on the code that GETS and PRODUCES the request...

otherwise if your site.com changes... you'll have to change ALL the data in the table :)

$name = $runrows['name'];

        $image = $runrows['image'];

        $hyperlink = $runrows['hyperlink'];

        $currency = $runrows ['currency'];

        $info = $runrows ['info'];

        $type = $runrows ['type'];

 

 

 

      echo "<table><tr><td>

 

if ($type == 'I') {

        echo '<img src='http://www.u-stack.com/Image.jpg'>';

} elseif ($type == 'V') {

        echo '<img src='http://www.u-stack.com/Video.jpg'>';

} elseif ($type == 'F') {

        echo '<img src='http://www.u-stack.com/File.jpg'>';

} elseif ($type == 'M') {

        echo '<img src='http://www.u-stack.com/Music.jpg'>';

}

      </td></tr></table>";

 

 

I tried it it seems to be echoing all the images and shows the if code behind them??

    echo "<table><tr><td>";

 

if ($type == 'I') {

        echo '<img src="http://www.u-stack.com/Image.jpg">';

} elseif ($type == 'V') {

        echo '<img src="http://www.u-stack.com/Video.jpg">';

} elseif ($type == 'F') {

        echo '<img src="http://www.u-stack.com/File.jpg">';

} elseif ($type == 'M') {

        echo "<img src="http://www.u-stack.com/Music.jpg">';

}

      echo "</td></tr></table>";

<?php

//get data

 

 

 

 

$button = $_GET['submit'];

$search = $_GET['search'];

 

if

 

(!$button)

echo "You didn't submit a keyword";

 

else

{

if (strlen($search)<=2)

echo "search term too short";

else

{

echo "<br><br><font color=white>you searched for <b>$search</b></font><hr size='1'>";

}

mysql_connect("localhost", "Master", "password");

mysql_select_db("ustackc1_Login");

 

 

//explode our search term

$search_exploded = explode(" ",$search);

foreach($search_exploded as $search_each)

{

//construct query

$x++;

if($x==1)

        $construct .= "keywords LIKE '%$search_each%'";

        else

        $construct .= "OR keywords LIKE '%$search_each%'";

}

 

                  //echo out $construct

$construct = "SELECT * FROM Upload WHERE $construct";

}              $run = mysql_query($construct);

                $foundnum =  mysql_num_rows($run);

 

if  ($foundnum==0)

echo "No Stacks Found";

else

{

    echo "$foundnum Stacks Found!<p>";

 

 

    while ($runrows = mysql_fetch_assoc($run))

    {

              //get data

        $name = $runrows['name'];

        $image = $runrows['image'];

        $hyperlink = $runrows['hyperlink'];

        $currency = $runrows ['currency'];

        $info = $runrows ['info'];

        $type = $runrows ['type'];

 

 

 

      echo "<table><tr><td>";

 

if ($type == 'I') {

        echo '<img src="http://www.u-stack.com/Image.jpg">';

} elseif ($type == 'V') {

        echo '<img src="http://www.u-stack.com/Video.jpg">';

} elseif ($type == 'F') {

        echo '<img src="http://www.u-stack.com/File.jpg">';

} elseif ($type == 'M') {

        echo '<img src="http://www.u-stack.com/Music.jpg">';

}

      echo "</td></tr></table>";

 

 

    }

}

 

?>

        $currency = $runrows ['currency'];

        $info = $runrows ['info'];

        $type = $runrows ['type'];

 

replace that for

 

        $currency = $runrows['currency'];

        $info = $runrows['info'];

        $type = $runrows['type'];

      echo "Type: $type";

 

save and upload then see what happens

        $currency = $runrows ['currency'];

        $info = $runrows ['info'];

        $type = $runrows ['type'];

 

 

should be

 

        $currency = $runrows['currency'];

        $info = $runrows['info'];

        $type = $runrows['type'];

 

thats identical apart from the spacing.. i changed the spacing still the images aren't appearing

        $currency = $runrows ['currency'];

        $info = $runrows ['info'];

        $type = $runrows ['type'];

 

replace that for

 

        $currency = $runrows['currency'];

        $info = $runrows['info'];

        $type = $runrows['type'];

      echo "Type: $type";

 

save and upload then see what happens

 

k i did that now it displays, represent my two images

 

Type: Type:

Archived

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

×
×
  • Create New...

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.