I have a situation where I need to compare 3 different select option fields of the same table. I compared 3 fields, 2 fields and 1 field. The corresponding results are displayed, but in one field condition (category) result are not displayed.
<?php
include'connect.php';
if(isset($_POST['submit']))
{
$qn=$_POST['location'];//LOCATION
$qn1=$_POST['category'];//CATEGORY
$qn2=$_POST['salary'];//SALARY
if(isset($_POST['location']))
{
$q2=mysql_query("SELECT * FROM job_posting where location='$qn'");
while($quew=mysql_fetch_array($q2))
{
echo $ans=$quew['title'];
}
}
else
{
$qir=mysql_query("SELECT * FROM job_posting where category='$qn1'");
while($quew=mysql_fetch_array($qir))
{
echo $gn=$quew['title'];
}
}
if(isset($_POST['location']) && isset($_POST['category']))
{
$q3=mysql_query("SELECT * FROM job_posting where location='$qn' && category='$qn1'");
while($quew=mysql_fetch_array($q3))
{
echo $ans2=$quew['title'];
}
}
if(isset($_POST['location']) && isset($_POST['category']) && isset($_POST['salary']))
{
$q4=mysql_query("SELECT * FROM job_posting where location='$qn' && category='$qn1' && minsalary='$qn2'");
while($quew=mysql_fetch_array($q4))
{
echo $ans3=$quew['title'];
}
}
if(isset($_POST['location']) || isset($_POST['category']) && isset($_POST['salary']))
{
$q5=mysql_query("SELECT * FROM job_posting where location='$qn' || category='$qn1' && minsalary='$qn2'");
while($quew=mysql_fetch_array($q5))
{
echo $ans4=$quew['title'];
}
}
if(isset($_POST['location']) && isset($_POST['category']) || isset($_POST['salary']))
{
$q7=mysql_query("SELECT * FROM job_posting where location='$qn' && category='$qn1' || minsalary='$qn2'");
while($quew=mysql_fetch_array($q7))
{
echo $ans5=$quew['title'];
}
}
}
?>
For a start, I can think of three things
Database Query => check if request the "category"
Database Type => utf8-bin, utf8-general-ci etc.. because is very important how are saved data.
Server file "php.ini" => check "default_charset" and "mbstring..." params, is important to manage string type data.
If you need more help only need send we more information
Try this:
if (!empty($_POST['location'])) {
$lq = "&& location='$_POST['location']'";
}
if (!empty($_POST['category'])) {
$cq = "&& category='$_POST[category]'";
}
if (!empty($_POST['salary'])) {
$sq = "&& minsalary='$_POST[salary]'";
}
$qr = mysql_query("SELECT * FROM job_posting WHERE 1 $lq $cq $sq");
while ($rs = mysql_fetch_array($qr)) {
// do whatever you want
}
I am comparing seven (7) boolean values 1 (true) or 0 (false) of a primary set and five (5) of a secondary set with an addition of 2 n/a choices. If all 12 come back as true, then the $class is set to Reuse. If the primary set is True and Secondary is false then it comes back as Resale. If any of the primary set comes back False, it's set to Repair. This is what I have so far, and don't get back any syntax errors, but the "class" is coming back incorrect.
<?php
$primary = false;
$class = null;
if ($_POST['poweradapter'] == "1"
&& $_POST['mobocpu'] == "1"
&& $_POST['memory'] == "1"
&& $_POST['harddrive'] == "1"
&& $_POST['screen'] == "1"
&& $_POST['battery'] == "1"
&& $_POST['hinge'] == "1")
{
$class = "Reuse";
$primary = true;
}
else
{
$class = "Repair or Recycle";
}
if ($primary
&& in_array($_POST['opticaldrive'], ["1", "2"])
&& in_array($_POST['floppydrive'], ["1", "2"])
&& $_POST['usb'] == "1"
&& $_POST['trackpad'] == "1"
&& $_POST['keyboard'] == "1")
{
/*
* "secondary" is implicit here, but we never did anything with the
* $secondary variable in the original script
*/
$class = "Reuse";
}
else
{
$class = ($primary) ? "Reuse" : "Repair or Recycle";
}
?>
Final working script
<?php
$primary = false;
$class = null;
if ($_POST['poweradapter'] == "1"
&& $_POST['mobocpu'] == "1"
&& $_POST['memory'] == "1"
&& $_POST['harddrive'] == "1"
&& $_POST['screen'] == "1"
&& $_POST['battery'] == "1"
&& $_POST['hinge'] == "1")
{
$primary = true;
$class = "Resale";
}
else
{
$class = "Repair or Recycle";
}
if ($primary && in_array($_POST['opticaldrive'], ["1", "2"])
&& in_array($_POST['floppydrive'], ["1", "2"])
&& $_POST['usb'] == "1"
&& $_POST['trackpad'] == "1"
&& $_POST['keyboard'] == "1")
{
$class = "Reuse";
}
?>
The main problem is that you're using double equals in the body of the first if statement.
$class == "Reuse";
$primary == "True";
should be
$class = "Reuse";
$primary = "True";
There are a lot of things you could do to make this more readable and maintainable, but the bug is the equality vs assignment issue.
Here is what I would do to make this easier to work with:
<?php
$primary = false;
$class = null;
if ($_POST['poweradapter'] == "1"
&& $_POST['mobocpu'] == "1"
&& $_POST['memory'] == "1"
&& $_POST['harddrive'] == "1"
&& $_POST['screen'] == "1"
&& $_POST['battery'] == "1"
&& $_POST['hinge'] == "1")
{
$class = "Reuse";
$primary = true;
}
else
{
$class = "Repair or Recycle";
}
if ($primary
&& in_array($_POST['opticaldrive'], ["1", "2"])
&& in_array($_POST['floppydrive'], ["1", "2"])
&& $_POST['usb'] == "1"
&& $_POST['trackpad'] == "1"
&& $_POST['keyboard'] == "1")
{
/*
* "secondary" is implicit here, but we never did anything with the
* $secondary variable in the original script
*/
$class = "Reuse";
}
else
{
$class = ($primary) ? "Resale" : "Repair or Recycle";
}
Hope this helps.
[Edit]
Since you're still having trouble with the logic itself, this is an ideal case for unit testing. Here's a little script you can run from the command line to test and fine tune your logic without having to worry about getting PHPUnit set up:
<?php
class MyService
{
public static $CLASS_REUSE = 'Reuse';
public static $CLASS_RESALE = 'Resale';
public static $CLASS_REPAIR_RECYCLE = 'Repair or Recycle';
public static function determineClassForInputParameters($params)
{
$primary = false;
$class = null;
if ($params['poweradapter'] == "1"
&& $params['mobocpu'] == "1"
&& $params['memory'] == "1"
&& $params['harddrive'] == "1"
&& $params['screen'] == "1"
&& $params['battery'] == "1"
&& $params['hinge'] == "1")
{
$primary = true;
}
if ($primary
&& in_array($params['opticaldrive'], ["1", "2"])
&& in_array($params['floppydrive'], ["1", "2"])
&& $params['usb'] == "1"
&& $params['trackpad'] == "1"
&& $params['keyboard'] == "1")
{
$class = self::$CLASS_REUSE;
}
else
{
$class = ($primary) ? self::$CLASS_RESALE : self::$CLASS_REPAIR_RECYCLE;
}
return $class;
}
}
class MyServiceTest
{
public function __construct()
{
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
assert_options(ASSERT_CALLBACK, array($this, 'assertHandler'));
}
public function determineClassForInputParametersShouldReturnReuse()
{
$params = [
'poweradapter' => 1,
'mobocpu' => 1,
'memory' => 1,
'harddrive' => 1,
'screen' => 1,
'battery' => 1,
'hinge' => 1,
'opticaldrive' => 1,
'floppydrive' => 1,
'usb' => 1,
'trackpad' => 1,
'keyboard' => 1
];
$class = MyService::determineClassForInputParameters($params);
if (assert($class == MyService::$CLASS_REUSE, 'Expected class ' . MyService::$CLASS_REUSE . ', found ' . $class))
{
echo "determineClassForInputParametersShouldReturnReuse Passed\n";
}
}
public function determineClassForInputParametersShouldReturnResale()
{
$params = [
'poweradapter' => 1,
'mobocpu' => 1,
'memory' => 1,
'harddrive' => 1,
'screen' => 1,
'battery' => 1,
'hinge' => 1,
'opticaldrive' => 1,
'floppydrive' => 1,
'usb' => 1,
'trackpad' => 1,
'keyboard' => 0
];
$class = MyService::determineClassForInputParameters($params);
if (assert($class == MyService::$CLASS_RESALE, 'Expected class ' . MyService::$CLASS_RESALE . ', found ' . $class))
{
echo "determineClassForInputParametersShouldReturnResale Passed\n";
}
}
public function determineClassForInputParametersShouldReturnRePairOrRecycle()
{
$params = [
'poweradapter' => 0,
'mobocpu' => 1,
'memory' => 1,
'harddrive' => 1,
'screen' => 1,
'battery' => 1,
'hinge' => 1,
'opticaldrive' => 1,
'floppydrive' => 1,
'usb' => 1,
'trackpad' => 1,
'keyboard' => 1
];
$class = MyService::determineClassForInputParameters($params);
if (assert($class == MyService::$CLASS_REPAIR_RECYCLE, 'Expected class ' . MyService::$CLASS_REPAIR_RECYCLE . ', found ' . $class))
{
echo "determineClassForInputParametersShouldReturnRePairOrRecycle Passed\n";
}
}
public function assertHandler($file, $line, $code, $desc = null)
{
echo "Assertion failed at $file:$line: $code";
if ($desc)
{
echo ": $desc";
}
echo "\n";
}
}
$tester = new MyServiceTest();
$tester->determineClassForInputParametersShouldReturnReuse();
$tester->determineClassForInputParametersShouldReturnResale();
$tester->determineClassForInputParametersShouldReturnRePairOrRecycle();
You may find it useful to adopt some of the techniques shown in the test, like encapsulating your logic in a service class to make it easier to test and use in multiple places, using static variables for your "class" names to prevent typo errors, etc. Or you can just tune your logic with this and copy it into your existing code.
It may seem like a lot of overhead, but the few minutes it takes to set up tests for your logic far outweighs the hours you can waste trying to figure it out by submitting requests through your front end.
Here try to first set primary and secondary flag to true or false, then compare for the conditions, below is the code snippet, hope this helps.
There are two problems
1. in the first if you are setting $primary as $primary == "True" instead of $primary="True" , so here instead of setting the primary flag, it is comparing whether $primary is true. same is the case with $class.
in the second if statement -$_POST['floppydrive']=="1" or"2" is always true , so it results in true block of if getting executed every time, here or "2", can be changed to - ($_POST['floppydrive']=="1" || $_POST['floppydrive']=="2")
This should get you desired result
<?php
//setting primary flag
if ($_POST['poweradapter']=="1" && $_POST['mobocpu']=="1" && $_POST['memory']=="1" && $_POST['harddrive']=="1" && $_POST['screen']=="1" && $_POST['battery']=="1" && $_POST['hinge']=="1"){
$primary = "True";
}else{
$primary = "False";
}
//setting secondary flag
if (($_POST['opticaldrive']=="1" || $_POST['opticaldrive']== "2") && ($_POST['floppydrive']=="1" || $_POST['floppydrive']== "2") && $_POST['usb']=="1" && $_POST['trackpad']=="1" && $_POST['keyboard']=="1"){
$secondary = "True";
}else{
$primary = "False";
}
//setting the class now
if ($primary == "True" && $secondary=="True") {
$class = "Reuse";
}
elseif ($primary == "True" && $secondary=="False") {
$class = "Resale";
}
else{
$class = "Repair or Recycle";
}
?>
output
Power Adapter: 1
Motherboard: 1
Memory: 1
Hard/SSD Drive: 1
Screen: 1
Battery: 0
Screen Hinge: 0
USB Ports: 0
Track/Touch Pad: 0
Keyboard: 0
Optical Drive: 0
Floppy Drive: 1
was added with an Class of Repair or Recycle
Is there any option to export only two columns in grocery-crud. I don't want to export all columns.
The correct way to do it is to use the getState() method
$state = $crud->getState();
if ($state == 'export' || $state == 'print') {
$crud->columns('first_name','last_name','email');
} else {
$crud->columns('first_name','last_name','email','phone','city','country');
}
Below you can find a full code example :
function example1()
{
$crud = new grocery_CRUD();
$crud->set_table('customers');
$crud->set_subject('Customer');
$crud->required_fields('first_name', 'last_name','email');
$state = $crud->getState();
if ($state == 'export' || $state == 'print') {
$crud->columns('first_name','last_name','email');
} else {
$crud->columns('first_name','last_name','email','phone','city','country');
}
$output = $crud->render();
$this->_example_output($output);
}
I have a setup where it favourites and retweets a specific tweet. For some reason, the code works for the favourites however retweets do not. Can anyone see the issue?
$method = 'statuses/retweet/'.$url[3];
$amt = "26";
$sub = rand(1,3);
$amt1 = $amt-$sub;
if($_POST['favorite'] == "true" || $_POST['favorite'] == "1"){
for ($x1=1; $x1<=$amt1; $x1++)
{
$content = $connection[$x1]->post('favorites/create', array('id' => $url[3]));
}
}
if($_POST['retweet'] == "true" || $_POST['retweet'] == "1"){
for ($x2=1; $x2<=$amt; $x2++)
{
$content = twitteroauth_row('statuses/retweet/'.$url[3], $connection[$x2]->post($method), $connection[$x2]->http_code);
}
}
have you tried declaring
$amt = 26
instead of
$amt="26"
EDIT:
for ($x2=1; $x2<=amt; $x2++)
{
$content = twitteroauth_row('statuses/retweet/'.$url[3], $connection[$x2]->post($method), $connection[$x2]->http_code);
}
you have used amt instead of $amt in the loop condition
I've been working on a piece of code that that pulls the name of a guild and with it the information of boss/monsters said guild has killed in an online game. There are many many monsters in this game and every one has three difficulty settings. I have managed to get the code to do what i want however it has an enormous amount of copy and paste and ive only done about 1/5 of the total amount of enteries. I really cant think how to make this code less of a giant bloat. This is the code for just one monster for the 3 difficulty settings as you can see it's alot just for one. there are probably another 60 of these!. Can anybody help me understand better ways to do this. Thanks!
$sql = 'SELECT * FROM `phpbb_profile_fields_data`';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
/////////////////////////////////// START - 8 MAN BONETHRASHER
$normal = '';
$hard = '';
$nightmare = '';
/////// START - CHECK NORMAL
if ($row['pf_kp_em_no_bonethr'] == '1')
{
$normal = ' <img src="/styles/subsilver2/theme/images/soap/no.png" />';
}
else if ($row['pf_kp_em_no_bonethr'] == '2')
{
$normal = '';
}
else if (is_null($row['pf_kp_em_no_bonethr']))
{
echo "Boss was set as NULL This should not happen!";
}
else
{
echo "Sosia messed up go hit him in the face.";
}
/////// END - CHECK NORMAL
/////// START - CHECK HARD
if ($row['pf_kp_em_ha_bonethr'] == '1')
{
$hard = ' <img src="/styles/subsilver2/theme/images/soap/ha.png" />';
}
else if ($row['pf_kp_em_ha_bonethr'] == '2')
{
$hard = '';
}
else if (is_null($row['pf_kp_em_ha_bonethr']))
{
echo "Boss was set as NULL This should not happen!";
}
else
{
echo "Sosia messed up go hit him in the face.";
}
/////// END - CHECK HARD
/////// START - CHECK NIGHTMARE
if ($row['pf_kp_em_kn_bonethr'] == '1')
{
$nightmare =' <img src="/styles/subsilver2/theme/images/soap/kn.png" />';
}
else if ($row['pf_kp_em_kn_bonethr'] == '2')
{
$nightmare = '';
}
else if (is_null($row['pf_kp_em_kn_bonethr']))
{
echo "Boss was set as NULL This should not happen!";
}
else
{
echo "Sosia messed up go hit him in the face.";
}
/////// END - CHECK NIGHTMARE
if ($normal == '' && $hard == '' && $nightmare == '')
{
}
else
{
$template->assign_block_vars('8m_bonethrasher', array(
'VAR1' => $row['pf_guild_name'],
'VAR2' => $normal,
'VAR3' => $hard,
'VAR4' => $nightmare,
));
}
}
$db->sql_freeresult($result);
I'm still slightly fuzzy at what you are trying to do, but I'll give helping you out a shot.
You could probably get away will creating a class that does all of this.
For example:
class checks {
public function checkBosses($normalBoss, $hardBoss, $nightmareBoss) {
$difficulties = array();
$difficulties['normal'] = array('boss' => $normalBoss);
$difficulties['hard'] = array('boss' => $hardBoss);
$difficulties['nightmare'] = array('boss' => $nightmareBoss);
foreach ($this->difficulties as $difficulty -> $boss) {
$this->difficulties[$difficulty]['result'] = checkDifficulty($boss['boss'], $difficulty);
}
$normal = $this->difficulties['normal']['result'];
$hard = $this->difficulties['hard']['result'];
$nightmare = $this->difficulties['nightmare']['result'];
if ($normal == '' && $hard == '' && $nightmare == '') {
return null;
} else {
return array(
'normal' => $normal,
'hard' => $hard,
'nightmare' => $nightmare,
);
}
}
protected function checkDifficulty($boss, $difficulty) {
if ($difficulty == 'normal') {
$image = ' <img src="/styles/subsilver2/theme/images/soap/no.png" />';
} else if ($difficulty == 'hard') {
$image = ' <img src="/styles/subsilver2/theme/images/soap/ha.png" />';
} else if ($difficulty == 'nightmare') {
$image = ' <img src="/styles/subsilver2/theme/images/soap/kn.png" />';
}
if ($boss == '1') {
return $image;
} else if ($boss == '2') {
return '';
} else if (is_null($boss)) {
echo "Boss was set as NULL This should not happen!";
} else {
echo "Sosia messed up go hit him in the face.";
}
}
}
Then all you would need to do is call:
$checkResult = checks::checkBosses($row['pf_kp_em_no_bonethr'], $row['pf_kp_em_ha_bonethr'], $row['pf_kp_em_kn_bonethr']);
if ($checkResult != null) {
$template->assign_block_vars('8m_bonethrasher', array(
'VAR1' => $row['pf_guild_name'],
'VAR2' => $normal,
'VAR3' => $hard,
'VAR4' => $nightmare,
));
}
If you can retrieve an array of bosses, you can do a foreach loop on them to run that same bit of code for each boss like this:
foreach ($bosses as $boss) {
//Full code to be repeated for each boss here
}