trying to write a proper if statement in php - php

i am having a hard time wrapping my head around how to do this... i have a select menu i want to display as default and a different one(s) if certain items appear in a shopping cart.
so say i sell dogs and cars. in the cart i have selected one of each. i want one select menu to appear if there are ever any cars in the cart. but if there are ever any dogs in the cart or cats for that matter i want a different select with different options to appear. so car_select would be the default if anything is in the cart...(i sell a lot of stuff) but if a dog appears in the cart then dog_select would replace it.
the way it is written now. the default is the last thing and that is what shows.
(see my comments)
so the code:
<?php
foreach($this->rows as $i => $row){
if (strstr('dogs',$row->dates)) {
$mydates = 'dogs';
} else {$mydates='default';}
//echo $row->dates; this spits out the correct values dogcar
}
//echo $mydates; with a dog and a car in the cart this returns default
//below functions i believe but i think the above is faulty
?>
<?php
if ($mydates =='dogs'){
//do something here;} else {
$type = $this->type;
foreach($this->extraFields[$type] as $fieldName => $oneExtraField) {
if ($fieldName != 'pickupdate-rosh'){; ?>
<tr class="hikashop_checkout_<?php echo $fieldName;?>_line">
<td class="key">
<?php echo $this->fieldsClass->getFieldName($oneExtraField);?>
</td>
<td>
<?php
echo $this->fieldsClass->display($oneExtraField,$this->$type->$fieldName,'data['.$type.']['.$fieldName.']');
?>
</td>
</tr>
<?php } ?>
<?php } ?>
<?php } ?>
i would like to always return car_select unless the presence of dogs or cats etc in the cart.
{edit: i just realized that is not the correct function to use to look for a string.}

It looks like you're setting $mydates, then overriding it (potentially). This is because you're setting one variable to one result for all iterations of your foreach loop, and the last iteration will be what the final variable value will be.
So set $mydates to a default and then change it if the condition is right.
$mydates='default';
foreach($this->rows as $i => $row){
if (strstr('dogs',$row->dates)) {
$mydates = 'dogs';
}
//echo $row->dates; this spits out the correct values dogcar
}

Related

CakePHP: Create array of checkboxes and update field based on whether it's set

I'm not sure if what I'm asking for is possible, but here's the scenario:
I have a foreach() loop that returns some values in HTML format:
<?php foreach($stuff as $s) { ?>
<tr>
<td><?php echo $s['Model']['data']; ?> </td>
</tr>
<?php } ?>
What I need to do is put a checkbox next to each $s['Model']['data'], preferably with $s['Model']['data'] as the label, and be able to set a value in a table based on whether the box is checked. So, my edit.ctp would look like this:
<?php foreach($stuff as $s) { ?>
<tr>
<td><?php echo $this->Form->input('flag', array('type'=>'checkbox, 'default'=>'', label=>' $s['Model']['data']', 'value'=>1 )); ?> </td>
</tr>
<?php } ?>
I have no trouble displaying the above. My problem is that I don't know how to tell my controller to update the necessary field if the labeled box is checked. Here's my first run at it:
if($this->request->save($this->request->data)) {
foreach($stuff as $s) {
$this->Model->id = $s['Model']['id'];
if(isset($this->request->data['FormName']['flag'])) {
$this->Model->set('flag', 1);
$this->Model->save('flag', true);
}
}
}
No errors at runtime, but nothing happens. The flag field in the Model I need to update remains = 0. I can debug $stuff and see data, so I know it's there. I also know that my edit function is hitting the foreach() loop. What might I be missing? Do I need to assign the checkbox a different value or check for whether it's set some other way?
It seems like you need to identify each checkbox to the current item by modifying the name of each checkbox, otherwise it will just use the value of the last checkbox for all the items.
<?php foreach($stuff as $i => $s) { ?>
<tr>
<td><?php echo $this->Form->input('flag'.$i, array('type'=>'checkbox, 'default'=>'', label=>' $s['Model']['data']', 'value'=>1 )); ?> </td>
</tr>
<?php } ?>
Then in the controller look for that specific flag
if($this->request->save($this->request->data)) {
foreach($stuff as $i => $s) {
$this->Model->id = $s['Model']['id'];
if(isset($this->request->data['FormName']['flag'.$i])) {
$this->Model->set('flag', 1);
$this->Model->save('flag', true);
}
}
}
Which assumes that ht elements in $stuff are always in the same order.

Displaying Search Fields

This may not be enough information for anyone to answer this, but I might just be missing something y'all would know by looking at the code.
In my site's admin panel, I can assign custom fields to categories (No. of beds, baths, whatever I create), but in order for the fields to show up on the Advanced Search page, you have to assign the field(s) to all of my sites categories.
I am trying to get the field(s) to show up all the time, even if they are only assigned to certain categories, not all of them.
Here is the code that renders the fields on the Advanced Search page, but again, only renders them if the field is assigned to all categories:
<?php
$get_catID = get_CATID($_GET['ad_cat_cat']);
if(empty($get_catID)) $get_catID = 0;
$get_catID = array($get_catID);
$arr = get_category_fields_without_vals($get_catID, 'no');
for($i=0;$i<count($arr);$i++)
{
echo '<tr>';
echo '<td>'.$arr[$i]['field_name'].$arr[$i]['id'].':</td>';
echo '<td>'.$arr[$i]['value'].'</td>';
echo '</tr>';
}
?>

PHP Populate dropdown menu values from database

I am a beginner trying to ouptut the values of dropdown menu from a database. I already automatically generate the values from database but the problem i am having is I want the the value that is being selected to be displayed.
Ideally im trying to update a Subject which I want to data to be displayed in an HTML page where each item could be updated. What i need is to be able to select the 'position' of the subject that is beeing selected.
Here is my code:
<p>Position:
<select name="position">
<?php
$sel_subject = get_all_subjects();
$subject_count = mysql_num_rows($sel_subject);
//$subject_count+1 because we are adding a subject
for($count=1; $count <= $subject_count+1; $count++) {
echo "<option value=\"{$count}\"";
if($sel_subject['position'] == $count) {
echo " selected='selected'";
}
echo ">{$count}</option>";
}
?>
</select>
You seem to be relying on $count to be some internal ID that remains static; given that it's just a monotonically advancing integer, this seems like a risky proposition. However, if you're comfortable with that, all you need to do is change the value in the last echo statement to be the subject name instead of $count. I would instead encourage you to use something meaningful to the database - for example, some people create an auto_increment field called subject_id and key off of that; others like to use UUIDs.
I would also generally suggest using a foreach loop instead of a for loop, as it tends to simplify the code (you don't have to worry about maintaining the counter, or creating fence post errors, etc.) Here's a brief example - I'm guessing a bit at what the actual data is that is available to you, hopefully you can extrapolate it to what you actually have.
<p>Position:
<select name="position">
<?php
$sel_subject = get_all_subjects();
foreach($sel_subject as $subject_num => $subject) {
echo '<option value="', $subject_num, '"';
if ($subject['selected']) {
echo 'selected="selected"';
}
echo '>', $subject['subject_name'], '</option>';
}

Displying Multiple Arrays Data

I am displaying the list of users in the select box. There are two types of users i-e selected users and non selected users. The values of these users are coming from the database in two arrays i-e One array contains selected users record and other array contains all users record. Now i want if the page loads the selected users record should be shown as selected in the select box and non selected users will be shown as non selected. Here is my code:
if ($selected != false ){
foreach ($selected as $select)
{}
foreach ($data as $rows) { echo $rows->id."<br />"; echo $select->id; ?>
<option value="<?php echo $rows->id; ?>" <?php if ($rows->id == $select->id) echo "selected";?>><?php echo $rows->username; ?></option>
<?php } } else{
foreach ($data as $rows) ?>
<option value="<?php echo $rows->id; ?>" <?php if ($rows->id == $select->id) ?>><?php echo $rows->username; ?></option>
<?php } ?>
The $selected object contains the selected users list and the $data object contains non selected/total number of users
If i am not wrong you have two array like
$arr1 = array(array('id'=>1,"name"=>'abc1',"user"=>'select'),array('id'=>2,"name"=>'abc2',"user"=>'select'),array('id'=>3,"name"=>'abc3',"user"=>'select'),array('id'=>4,"name"=>'abc4',"user"=>'select'),array('id'=>5,"name"=>'abc5',"user"=>'select'),array('id'=>6,"name"=>'abc6',"user"=>'select'),array('id'=>7,"name"=>'abc7',"user"=>'select'));
$arr2 = array(array('id'=>8,"name"=>'abc8',"user"=>'noselect'),array('id'=>9,"name"=>'abc9',"user"=>'noselect'),array('id'=>10,"name"=>'abc10',"user"=>'noselect'),array('id'=>11,"name"=>'abc11',"user"=>'noselect'),array('id'=>12,"name"=>'abc12',"user"=>'noselect'),array('id'=>13,"name"=>'abc13',"user"=>'noselect'),array('id'=>14,"name"=>'abc14',"user"=>'noselect'));
$arraymerege = array_merge($arr1,$arr2); // Merge the array into one
check out the html it display the user selected. i'll just put the condition under select box if user is noselect then it show selected. Please check the image for loop
you are closing your first foreach before you actually get inside the loop...try this. (rewritten with a little shortcode added)
<select name="users" type="multiple">
<?php
//no need to write $selected == false,
//this is the same, a ! will compare this var to false
//also i use : instead because its much nicer to read i.m.o
//its also a little easier to tell the difference from a closing
//if and foreach this way if you have a lot of nested comparisons.
if (!$selected) :
//you want to loop through all rows first.
foreach ($data as $rows) :
//now look at each selected user per user
foreach ($selected as $select) : ?>
//here is where you make the comparison. no need for any other loops
//this is called a turnary comparison. its basically short code for
//if(blah) else this;
//read up on it here:
// http://www.php.net/manual/en/language.operators.comparison.php
//the <?= is the same as <?php echo. This does require for you to have
//short codes turned on in your main php.ini. It usually is though.
<option value="<?= $rows->id; ?>
<?= (($rows->id == $select->id) ? 'selected' :''); ?>
<?= $rows->username; ?>
</option>
endforeach;
endif;
?>
</select>
This is all the code you need. You should only require two loops. The very first loop would iterate through each user, and the nested loop would then compare each of those users to each selected user. Also, the ability to write cleaner code requires planning. I would suggest investing in a white board or a notepad to draw out flow charts, diagrams etc before you start writing your code. You will get a basic picture in your head. There are always 100 million ways to do one thing, but only one way that is right for you. And its up to you to find that way.
From how I read the question, he is showing all users and highlighting the selected ones in a multiple select box. He has multiple selected users so you couldn't merge the arrays together. You would have to compare each user in the table to each selected user then decide if you need to highlight that user or not.

Not understanding how to add cart into PHP

I am stuck on this code. I am making a web page and on the side there is a place for a cart. And with the you should be able to click on an item and add it to cart. Well I am having trouble getting it to add it to cart. Can someone help me understand what I should be doing. I have been working on it for a few days and no matter what I am doing nothing is working. If i get the code to show you have 0 in your cart it wont add anything if i try to put it in the cart.
<h1>Cart Contents?</h1>
<div class="p2">
<?php
// Get all the categories and
// link them to category.php.
// Define and execute the query:
$q = 'SELECT category_id, category FROM categories ORDER BY category';
$r = mysqli_query($dbc, $q);
// Fetch the results:
while (list($fcid, $fcat) = mysqli_fetch_array($r, MYSQLI_NUM)) {
// Print as a list item.
echo "<li>$fcat</li>\n";
if($_SERVER['PHP_SELF']!="CART FILE"){
echo "<h1>Cart Contents</h1>";
echo "<div class=\"p2\">";
$itemCount=X;
foreach($_SESSION['cart'] as X=>X){
for($i=0;$i<count(X);$i++){
$itemCount+=X;
}
}
echo "You have ".$itemCount." total items in your cart.";
echo "</div>\n";
} // End of while loop.
?>
<h1>Specials?</h1>
<div class="p2">
<p>Maybe place specials or new items or related items here.</p>
</div>
</div>
<div class="content">
Ok here is a link to what the cart should do if you look over to the side it should do what that one is doing.
http://www.programmerskit.com/advPHP/ch5/
Shouldn't
$itemCount=X;
foreach($_SESSION['cart'] as X=>X){
for($i=0;$i<count(X);$i++){
$itemCount+=X;
}
}
just be:
$itemCount = count($_SESSION['cart']);
I can't otherwise figure out what that code is supposed to be doing.
Also, that code that outputs the cart appears to be in a while loop outputting each item category, so you will be displaying the cart multiple times, which I can only assume is not desired functionality.
Also, another poster made a point about the invalid use of X as a constant, which is also a good point.
You've got a bare X used all over the place. While saying
$somevar = X;
would be legitimate if you'd already done define('X', 'somevalue') previously, this next one
foreach($_SESSION['cart'] as X=>X){
is completely invalid. You cannot assign new values to a defined constant, let alone try to assign TWO different values at the same time
foreach($_SESSION['cart'] as $key => $value)
is how that particular bit of code should be.

Categories