...

View Full Version : Php if statement



bucket
10-22-2009, 11:55 PM
I am looking for a php if statement that is basicly like this:

Like if the value of a row in a column table is 1 then show this, if the value is 2 then show this.

Can someone help me out and make one for me.

I think its simple and not many lines.


+---------+
| P_style |
+---------+
| 2 |
+---------+

Like if its that value then echo something.
If the value is 1 for P_style then display something else.

oracleguy
10-23-2009, 12:02 AM
What you are looking for is the switch statement (http://us.php.net/manual/en/control-structures.switch.php).

bucket
10-23-2009, 12:09 AM
Can you show me how to make that with connecting to a mysql database and getting it from a table.

Thanks for figuring out what I mean. :)

BinaryX
10-23-2009, 12:22 AM
You can connnect to mysql via this script(also look at the switch statement):




mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("my_db") or die(mysql_error());

$result = mysql_query("SELECT col1 FROM mytable")
or die(mysql_error());

$row = mysql_fetch_row($result);
$col = $row[0];


switch($col){

case 1: echo'The value of the column is 1 ! '; break;
case 2: echo'The value of the column is 2 ! '; break;

}






This should work, it is untested though.

bucket
10-23-2009, 12:38 AM
Thanks.

So I edited to fit my style.



<?php
include("inc/config.php");
$result = mysql_query("SELECT style FROM website")
or die(mysql_error());

$row = mysql_fetch_row($result);
$col = $row[0];

switch($col)
{
case 1: echo "value1";
case 2: echo "value2";
}
?>

Now If someone can make a dropdown for me that edits the value on it.

Like this:

<select>
<option value="1">First</option>
<option value="2">second</option>
<option value="3">third</option>
</select>

and the value that you pic will be updated in the database. So if you pick third and click submit, the value that will be in the database would be 3.

Can someone help me out with that?

bucket
10-23-2009, 01:04 AM
Bump.
If someone can help me out.

karlosio
10-23-2009, 03:46 AM
<?php
include("inc/config.php");

if(isset($_POST['choice']))
{
$choice = $_POST['choice'];
$result = mysql_query("SELECT style FROM website WHERE column = '$choice'")
or die(mysql_error());

$row = mysql_fetch_row($result);
$col = $row[0];

switch($col)
{
case 1: echo "value1";
case 2: echo "value2";
}
}
?>

<form method="post" action="">
<select name="choice">
<option value="1">First</option>
<option value="2">second</option>
<option value="3">third</option>
</select>
<input type="submit" value="submit" />
</form>

bucket
10-24-2009, 02:04 PM
I coded up something like this:



<form action="...">
<select name="value">
<option value="1">First</option>
<option value="2">second</option>
<option value="3">third</option>
</select>
<input type="submit" name="submit">
</form>


And some php code to handle it


$allowed = array(1, 2, 3);
$default = 1;
if (isset($_POST['submit'])) {
$value = in_array($_POST['value'], $allowed) ? $_POST['value'] : $default;
$query = "UPDATE your_table SET style='$value' LIMIT 1";
mysql_query($query) or die(mysql_error());
}


Is that correct?

bucket
10-24-2009, 02:13 PM
Okay I have used your idea:


<?php
include("inc/config.php");

if(isset($_POST['choice']))
{
$choice = $_POST['choice'];
$result = mysql_query("SELECT style FROM website WHERE style = '$choice'")
or die(mysql_error());

$row = mysql_fetch_row($result);
$col = $row[0];

switch($col)
{
case 1: echo "value1";
case 2: echo "value2";
case 3: echo "value3";
}
}
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="choice">
<option value="1">First</option>
<option value="2">second</option>
<option value="3">third</option>
</select>
<input type="submit" value="submit" />
</form>

The thing is I want more than 2 cases. How would I do that?

bucket
10-24-2009, 02:15 PM
Also It is not changing the value the the row to what i selected it to be in the dropdown and clicked submit.

bucket
10-24-2009, 02:51 PM
Fixed!


<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

include("inc/config.php");
$allowed = array(1, 2, 3);
$default = 1;
if (isset($_POST['submit'])) {
$value = in_array($_POST['value'], $allowed) ? $_POST['value'] : $default;
$query = "UPDATE website SET style='$value' LIMIT 1";
mysql_query($query) or die(mysql_error());
}
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="value">
<option disabled selected value="0">Choose a style</option>
<option value="1">first</option>
<option value="2">second</option>
<option value="3">third</option>
</select>
<input type="submit" name="submit">
</form>



And the one to show what it picked:

<?php
include("inc/config.php");
$result = mysql_query("SELECT style FROM website")
or die(mysql_error());

$row = mysql_fetch_row($result);
$col = $row[0];

switch($col)
{
case 1:
echo "value1 selected";
break;
case 2:
echo "value2 selected";
break;
case 3:
echo "value3 selected";
break;
}
?>

Thanks for the help!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum