berry05 Posted December 10, 2008 Share Posted December 10, 2008 i'm trying to make it so that if a value of a field is 0 is echos text but if its value is 1 it does something else...any help? Quote Link to comment https://forums.phpfreaks.com/topic/136425-solved-value-and-field-help-in-mysql/ Share on other sites More sharing options...
premiso Posted December 10, 2008 Share Posted December 10, 2008 <?php if ($field == 0) { echo "Some Text"; }elseif ($field == 1) { echo 'Do something else!'; } maq just modified my post. It was correct all along =) Quote Link to comment https://forums.phpfreaks.com/topic/136425-solved-value-and-field-help-in-mysql/#findComment-711976 Share on other sites More sharing options...
Maq Posted December 10, 2008 Share Posted December 10, 2008 if ($field == 0) { echo 'Some Text'; //need single }elseif ($field == 1) { echo 'Do something else!'; //and another! } Quote Link to comment https://forums.phpfreaks.com/topic/136425-solved-value-and-field-help-in-mysql/#findComment-711979 Share on other sites More sharing options...
phpSensei Posted December 10, 2008 Share Posted December 10, 2008 <?php if ($field == 0) { echo 'Some Text'; //need single }elseif ($field == 1) { echo 'Do something else!'; //and another! } lol why post the same thing as premiso. Quote Link to comment https://forums.phpfreaks.com/topic/136425-solved-value-and-field-help-in-mysql/#findComment-711986 Share on other sites More sharing options...
justinh Posted December 10, 2008 Share Posted December 10, 2008 <?php include("dbconfig.php"); //replace with your db connection file $query = mysql_query("SELECT * FROM tbl_name"); //replace tbl_name with your tbl while($getinfo = mysql_fetch_array($query)){ if($getinfo['rowhere'] == 0){ echo "row here equals 0" ; } if($getinfo['rowhere'] == 1){ echo "row here equals 1"; } } ?> I think this is what you want, my php isn't "great" but im learning and like to try and help people out. If there is any errors i apologize =) Quote Link to comment https://forums.phpfreaks.com/topic/136425-solved-value-and-field-help-in-mysql/#findComment-711988 Share on other sites More sharing options...
Maq Posted December 10, 2008 Share Posted December 10, 2008 if ($field == 0) { echo 'Some Text'; //need single }elseif ($field == 1) { echo 'Do something else!'; //and another! } lol why post the same thing as premiso. I didn't, take a look at the comments Quote Link to comment https://forums.phpfreaks.com/topic/136425-solved-value-and-field-help-in-mysql/#findComment-711992 Share on other sites More sharing options...
redarrow Posted December 10, 2008 Share Posted December 10, 2008 my version.... <?php <?php include("dbconfig.php"); //replace with your db connection file $query = mysql_query("SELECT * FROM tbl_name"); //replace tbl_name with your tbl while($getinfo = mysql_fetch_assoc($query)){ switch($getinfo['rowhere']) { case '0': $x="Some Text"; break; case '1': $x='Do something else!'; break; } } echo $x; ?> Quote Link to comment https://forums.phpfreaks.com/topic/136425-solved-value-and-field-help-in-mysql/#findComment-712004 Share on other sites More sharing options...
phpSensei Posted December 10, 2008 Share Posted December 10, 2008 My Version... <?php //....mysql crap $field = ($row['field'] == 0) ? 0 : $row['field']; if($field == 0){ // do something }else{ // do something else duh } ?> Quote Link to comment https://forums.phpfreaks.com/topic/136425-solved-value-and-field-help-in-mysql/#findComment-712006 Share on other sites More sharing options...
Maq Posted December 10, 2008 Share Posted December 10, 2008 Why is everyone connecting to a database? He only wanted an if elseif statement... Use premiso's, or rather use my updated version of premiso's. Quote Link to comment https://forums.phpfreaks.com/topic/136425-solved-value-and-field-help-in-mysql/#findComment-712013 Share on other sites More sharing options...
phpSensei Posted December 10, 2008 Share Posted December 10, 2008 Why is everyone connecting to a database? He only wanted an if elseif statement... Use premiso's, or rather use my updated version of premiso's. The sould purpose of everyone doing that is because is said the value of a field, not a var. Also for the post count Quote Link to comment https://forums.phpfreaks.com/topic/136425-solved-value-and-field-help-in-mysql/#findComment-712018 Share on other sites More sharing options...
premiso Posted December 10, 2008 Share Posted December 10, 2008 Why is everyone connecting to a database? He only wanted an if elseif statement... Use premiso's, or rather use my updated version of premiso's. MySQL is in the title....but here is my version! <?php $result = mysql_query("SELECT field FROM tbl_name WHERE field = 1"); //replace tbl_name with your tbl if (mysql_num_rows($result) == 0) { echo 'Do this here'; }else { echo 'Do this other thing here!'; } ?> =) Quote Link to comment https://forums.phpfreaks.com/topic/136425-solved-value-and-field-help-in-mysql/#findComment-712023 Share on other sites More sharing options...
justinh Posted December 10, 2008 Share Posted December 10, 2008 Hey phpsenei, I was wondering about this line that you put in your code ($row['field'] == 0) ? 0 : $row['field']; could you explain the " ? 0 : " part or give me a link to read up on it? Quote Link to comment https://forums.phpfreaks.com/topic/136425-solved-value-and-field-help-in-mysql/#findComment-712025 Share on other sites More sharing options...
premiso Posted December 10, 2008 Share Posted December 10, 2008 Hey phpsenei, I was wondering about this line that you put in your code ($row['field'] == 0) ? 0 : $row['field']; could you explain the " ? 0 : " part or give me a link to read up on it? Its a shortened if/else statement. If the field is equal to 0 set it to 0 else set it to $row['field']; Quote Link to comment https://forums.phpfreaks.com/topic/136425-solved-value-and-field-help-in-mysql/#findComment-712031 Share on other sites More sharing options...
berry05 Posted December 10, 2008 Author Share Posted December 10, 2008 My Version... <?php //....mysql crap $field = ($row['field'] == 0) ? 0 : $row['field']; if($field == 0){ // do something }else{ // do something else duh } ?> that one worked!! thank you! Quote Link to comment https://forums.phpfreaks.com/topic/136425-solved-value-and-field-help-in-mysql/#findComment-712033 Share on other sites More sharing options...
phpSensei Posted December 10, 2008 Share Posted December 10, 2008 Hey phpsenei, I was wondering about this line that you put in your code ($row['field'] == 0) ? 0 : $row['field']; could you explain the " ? 0 : " part or give me a link to read up on it? Hey friend, glad to help. It is another conditional operator called ternary, prevents the whole curly bracelet hassle http://ca.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary Quote Link to comment https://forums.phpfreaks.com/topic/136425-solved-value-and-field-help-in-mysql/#findComment-712036 Share on other sites More sharing options...
Maq Posted December 11, 2008 Share Posted December 11, 2008 It's called a ternary operator. TRUE('?') ELSE(':'). $field = ($row['field'] == 0) ? 0 : $row['field']; Is the same exact thing as: if($row['field'] == 0) { $field = 0; } else { $field = $row['field']; } Quote Link to comment https://forums.phpfreaks.com/topic/136425-solved-value-and-field-help-in-mysql/#findComment-712148 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.