How can I get this to display in 1 loop - php

In the backend of my website, I have added more categories as follows
<strong>Tour category : </strong><br />
<select name="category_id">
<?php
foreach($category_lists as $list){
if($list['id'] == $arr_collect['category_id']){
$sel = ' selected="selected" ';
}else{
$sel = null;
}
echo '<option value="'.$list['id'].'"'.$sel.'>'.$list['name'].'</option>';
}
?>
</select>
<br clear="all" />
<strong>Tour category 2: </strong><br />
<select name="category_id2">
<option value="">Select Another</option>
<?php
foreach($category_lists as $list){
if($list['id'] == $arr_collect['category_id2']){
$sel = ' selected="selected" ';
}else{
$sel = null;
}
echo '<option value="'.$list['id'].'"'.$sel.'>'.$list['name'].'</option>';
}
?>
</select>
<br clear="all" />
<strong>Tour category 3: </strong><br />
<select name="category_id3">
<option value="">Select Another</option>
<?php
foreach($category_lists as $list){
if($list['id'] == $arr_collect['category_id3']){
$sel = ' selected="selected" ';
}else{
$sel = null;
}
echo '<option value="'.$list['id'].'"'.$sel.'>'.$list['name'].'</option>';
}
?>
</select>
In the front end, I have this
<?php
$sql_ci = "SELECT * FROM location WHERE parent_id <> 0 ORDER BY parent_id, sort ";
$result_ci = mysql_query($sql_ci);
$cities = $Manager->fetchAssoc($result_ci); //peek_array($cities);
if(!empty($cities)){
foreach($cities as $city){
$i = 1; // for active category
$sql_count = "SELECT * FROM tour t, tour_category c
where t.location_id = {$city['id']} and t.type = 'P'
group by t.category_id AND t.category_id2"; //echo $sql_count.'<br />';
$result_count = mysql_query($sql_count);
$count = $Manager->fetchAssoc($result_count);
?>
<ul class="tours-accord tour-private" id="tourcity<?php echo $city['id'];?>">
<?php
$sql_ca = "SELECT * FROM tour_category ORDER BY sort";
$result_ca = mysql_query($sql_ca);
$categories = $Manager->fetchAssoc($result_ca);
if(!empty($categories)){
foreach($categories as $cat){
$sql_tour = "SELECT t.* FROM tour t
WHERE
t.touronline = 'yes'
AND t.category_id = {$cat['id']}
AND t.location_id = {$city['id']}
and t.type = 'P'
ORDER BY t.tourcode";
$result_tour = mysql_query($sql_tour);
$tours = $Manager->fetchAssoc($result_tour);
if(!empty($tours)){
?>
<li style="border-bottom:1px solid gray;">
<?php echo $city['name']; ?> - <?php echo $cat['name']; //echo ' '.$i.' ';?>
<ul>
<?php foreach($tours as $tour){
if(!empty($tour['intro_image'])){
$img = $tour['intro_image'];
}else{
$img = 'default.jpg';
}
?>
<a href="tour-details.php?code=<?php echo $tour['tourcode'];?>" class="tourname">
<li class="<?php if($i == count($count)){echo 'active';} ?>">
<img src="uploaded-items/tour-introimage/<?php echo $img; ?>" width="150" height="100" />
<span class="tourname"><?php echo $tour['tourname'].'(Code: '.$tour['tourcode'].')'; ?></span>
<p><?php echo $tour['intro']; ?></p>
<div class="home-tour-info">
<img src="images/btn-more-info-green-dark.jpg" alt="More info" class="moreinfo"/>
<div class="home-tour-price"><?php echo $tour['promotion_label']; ?></div>
</div>
</li></a>
<?php } ?>
</ul>
</li>
<?php $i++;}}} //each category ?>
The way I made it work was post the last bit 3 times and changed category_id to category_id2, category_id3. But this displayed duplicate categories.
How can I make all this just in 1 loop instead of 3?

Your backend loop is like wise as follows, as a optimising code,
<?
$numcount = 3;
for($i=0;$i<$numcount;$i++) { ?>
<strong>Tour category <?=$i+1?>: </strong><br />
<select name="category_id<?=$i+1?>">
<?php
foreach($category_lists as $list){
if($list['id'] == $arr_collect['category_id'.($i+1)]){
$sel = ' selected="selected" ';
}else{
$sel = null;
}
echo '<option value="'.$list['id'].'"'.$sel.'>'.$list['name'].'</option>';
}
?>
</select>
<br clear="all" />
<? } ?>

Related

How to show just one time data while use foreach

I made a foreach to find out the ID of the item contained because there is a product that has a special packaging, I did a foreach like this
else if($value['id'] != 13){
foreach ($_packaging as $key => $value) {
if($value->p_id != 7){
$img_first = '';
$p_name = "p_name_" . $this->config->item("language");
$p_price = "p_price_" . $_currency;
$p_info = "p_info_" . $this->config->item("language");
if($key == 0){
$img_first = 'data-img-class="first"';
}
$text_price = "FREE";
if($value->$p_price != 0){
$text_price = currency_display($value->$p_price);
}
if($value->p_manage_stock){
if(($value->p_stock_qty - $value->p_stock_used) >= $_total_qty){
?>
<option data-img-label="<?php echo "<p class='packaging-price'>".$text_price."</p>" . "<br> <span class='packaging-info'>".$value->$p_info."</span>"; ?>" data-img-src="<?php echo base_url('assets/img/packaging/'.$value->p_image); ?>" <?php echo $img_first; ?> data-price="<?php echo $value->$p_price; ?>" value="<?php echo $value->p_id; ?>"> <?php echo $text_price; ?> </option>
<?php
}
}else{
?>
<option data-img-label="<?php echo "<p class='packaging-price'>".$text_price."</p>" . "<br> <span class='packaging-info'>".$value->$p_info."</span>"; ?>" data-img-src="<?php echo base_url('assets/img/packaging/'.$value->p_image); ?>" <?php echo $img_first; ?> data-price="<?php echo $value->$p_price; ?>" value="<?php echo $value->p_id; ?>"> <?php echo $text_price; ?> </option>
<?php
}
}
}
}
The Full code i save in pastebin
Full code in this link
I have 2 different products and the id != 13
but the problem is when there are 2 different products ID in the dining cart, the packaging options look double as below
What if I only want to display it once? because the id from multiple data packaging is the same?

Magento: How to get sub category of current category by level and so on

I have a following code that shows category by level, but I have to show all the sub categories from that specific category.
<?php
$_cat = new Mage_Catalog_Block_Navigation();
$currentCat = $_cat->getCurrentCategory();
$subCats = Mage::getModel('catalog/category')->load($currentCat->getId())- >getChildren();
$subCatIds = explode(',',$subCats);
?>
<?php ////////////////////////level-3///////////////////////////// ?>
<?php $category = Mage::registry('current_category');
$category->getParentCategories();
if ( $category->getLevel() == 3 ) : ?>
<div class="cat_drop_ser_wrap">
<?php $currentCat = Mage::getModel('catalog/category')->load($currentCat- >getId()) ?>
<select class="select_class" onchange="window.location.href=this.value">
<option value="#">-Select</option>
<?php foreach($subCatIds as $subCatId): ?>
<?php $subCat = Mage::getModel('catalog/category')->load($subCatId); ?>
<?php if($subCat->getIsActive()): ?>
<option value="<?php echo $subCat->getUrl() ?>">
<?php echo $subCat->getName(); ?>
</option>
<?php endif; ?>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?><!--if--level-3-->
Thanks in advance.
Ok in case anyone needs it following will show the sub categories and so on:
<?php //////sub////// ?>
<?php
$category_levels = Mage::getModel('catalog/category')->load($category->getId());
$subcategories = $category_levels->getChildrenCategories();
if (count($subcategories) > 0){
foreach($subcategories as $subcategory){
$category_levels_two = Mage::getModel('catalog/category')->load($subcategory->getId());
$subcategoriess = $category_levels_two->getChildrenCategories();
if (count($subcategoriess) > 0){
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_category', 'category_dropdown_label');
$text = $category_levels_two->getCategoryDropdownLabel();
echo '<div class="empty_serch_select_1 cat_drop_ser_wrap 2">';
echo '<label>' . $text . '</label>';
echo '<select disabled="disabled">';
echo '<option value="#">-Select</option>';
echo '<select>';
echo '</div>';
break;
}
}
}
?>
<?php //////sub-sub////// ?>
<?php
$category_levels = Mage::getModel('catalog/category')->load($category->getId());
$subcategories = $category_levels->getChildrenCategories();
echo '<div class="empty_serch_select_2 cat_drop_ser_wrap 3">';
if (count($subcategories) > 0){
foreach($subcategories as $subcategory){
$category_levels_two = Mage::getModel('catalog/category')->load($subcategory->getId());
$subcategoriess = $category_levels_two->getChildrenCategories();
if (count($subcategoriess) > 0){
foreach($subcategoriess as $subcategorys){
$category_levels_three = Mage::getModel('catalog/category')->load($subcategorys->getId());
$subcategoriesss = $category_levels_three->getChildrenCategories();
if (count($subcategoriesss) > 0){
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_category', 'category_dropdown_label');
$text = $category_levels_three->getCategoryDropdownLabel();
echo '<label>' . $text . '</label>';
break;
}
}
}
}
}
echo '<select disabled="disabled">';
echo '<option value="#">-Select</option>';
echo '<select>';
echo '</div>';
?>
<?php //////sub-sub-sub////// ?>
<?php
$category_levels = Mage::getModel('catalog/category')->load($category->getId());
$subcategories = $category_levels->getChildrenCategories();
echo '<div class="empty_serch_select_2 cat_drop_ser_wrap 3">';
if (count($subcategories) > 0){
foreach($subcategories as $subcategory){
$category_levels_two = Mage::getModel('catalog/category')->load($subcategory->getId());
$subcategoriess = $category_levels_two->getChildrenCategories();
if (count($subcategoriess) > 0){
foreach($subcategoriess as $subcategorys){
$category_levels_three = Mage::getModel('catalog/category')->load($subcategorys->getId());
$subcategoriesss = $category_levels_three->getChildrenCategories();
if (count($subcategoriesss) > 0){
foreach($subcategoriesss as $subcategorys){
$category_levels_three = Mage::getModel('catalog/category')->load($subcategorys->getId());
$subcategoriessss = $category_levels_three->getChildrenCategories();
if (count($subcategoriessss) > 0){
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_category', 'category_dropdown_label');
$text = $category_levels_three->getCategoryDropdownLabel();
echo '<label>' . $text . '</label>';
break;
}
}
}
}
}
}
}
echo '<select disabled="disabled">';
echo '<option value="#">-Select</option>';
echo '<select>';
echo '</div>';
?>

Menu Items Not Closing?

I'm coding a staff panel but I'm stuck I've added a menu (navigation) but I'm stuck on how to go about opening it when clicked and closing it when clicked if open.
Here is the code i have so far;
<ul id="menu" class="nav">
<?php
$url = $_GET['url'] ? $core->clean($_GET['url']) : 'core.home';
$query3 = $db->query("SELECT * FROM menu WHERE url = '{$url}'");
$array3 = $db->assoc($query3);
if (!$array3['usergroup']) {
$array3['usergroup'] = "invalid";
}
$query = $db->query("SELECT * FROM usergroups ORDER BY weight ASC");
while ($array = $db->assoc($query)) {
if (in_array($array['id'], $user->data['uGroupArray'])) {
?>
<div class="menustyle" onclick="Radi.menuToggle('<?php echo $array['id']; ?>');">
<div class="menutext"><?php echo $array['name']; ?></div>
</div>
<ul>
<li class="menuitems"<?php if ($array['id'] != $array3['usergroup']) { ?> onclick="Radi.menuToggle('<?php echo $array['id']; ?>');" style="display: none;"<?php } ?> id="mitems_<?php echo $array['id']; ?>">
<?php
$query2 = $db->query("SELECT * FROM menu WHERE usergroup = '{$array['id']}' AND visible = '1' ORDER BY weight ASC");
$i = "a";
while ($array2 = $db->assoc($query2)) {
?>
<li>
<?php echo $array2['text']; ?>
</li>
<?php
$i++;
if ($i == "c") {
$i = "a";
}
}
?>
</ul>
</li>
<?php
}
}
?>
</li>
</ul>
</div>
And this is the defualt code when you download radipanel;
<div style="float: left; width: 200px;">
<?php
$url = $_GET['url'] ? $core->clean($_GET['url']) : 'core.home';
$query3 = $db->query("SELECT * FROM menu WHERE url = '{$url}'");
$array3 = $db->assoc($query3);
if (!$array3['usergroup']) {
$array3['usergroup'] = "invalid";
}
$query = $db->query("SELECT * FROM usergroups ORDER BY weight ASC");
while ($array = $db->assoc($query)) {
if (in_array($array['id'], $user->data['uGroupArray'])) {
?>
<div class="box">
<div class="square menu" style="background: #<?php echo $array['colour']; ?>;" onclick="Radi.menuToggle('<?php echo $array['id']; ?>');">
<img id="menutoggle_<?php echo $array['id']; ?>" class="menutoggle" src="_img/<?php echo ( $array['id'] != $array3['usergroup'] ) ? 'plus' : 'minus'; ?>_white.png" alt="Toggle" align="right" />
<strong><?php echo $array['name']; ?></strong>
</div>
<div class="menuitems"<?php if ($array['id'] != $array3['usergroup']) { ?> style="display: none;"<?php } ?> id="mitems_<?php echo $array['id']; ?>">
<?php
$query2 = $db->query("SELECT * FROM menu WHERE usergroup = '{$array['id']}' ORDER BY weight ASC");
$i = "a";
while ($array2 = $db->assoc($query2)) {
?>
<a href="<?php echo $array2['url']; ?>" class="<?php echo $i; ?>">
<?php echo $array2['text']; ?>
</a>
<?php
$i++;
if ($i == "c") {
$i = "a";
}
}
?>
</div>
</div>
<?php
}
}
?>
</div>
So any ideas on why my code is not doing it?

Set option selected attribute from sql return value

I have this function that is supposed to echo the full option values that is returned from the while loop. The problem is that the SELECT attribute is not being set. So the list options default to the first option even when a different id variable is passed.
function popselect($pos){
$pquery = "SELECT * FROM `players` WHERE `position` = '$pos'";
$presult = mysql_query($pquery)
or die ("Query failed: " . mysql_error());
$tquery = "SELECT * FROM teams WHERE `selection_id` = '$id'";
$tresult = mysql_query($tquery)
or die ("Query failed: " . mysql_error());
$trow = mysql_fetch_array($tresult);
while (($prow = mysql_fetch_array($presult)) != null){
$fullname = $prow['name'].' '.$prow['surname'];
$selected = "";
if ($fullname == $trow['f']){
$selected = 'selected="selected"';}
if ($fullname == $trow['gk']){
$selected = 'selected="selected"';}
if ($fullname == $trow['ld']){
$selected = 'selected="selected"';}
if ($fullname == $trow['rd']){
$selected = 'selected="selected"';}
if ($fullname == $trow['lm']){
$selected = 'selected="selected"';}
if ($fullname == $trow['rm']){
$selected = 'selected="selected"';}
echo "<option value='{$fullname}' ".$selected.">".$fullname."</option>";
}
}
The html is as follows:
<fieldset>
<ul>
<li>
<div id="l"><label for="gk">Goalkeeper:</label></div>
<div id="s"><select name="gk">
<?php popselect(Goalkeeper)?>
</select></div>
</li>
<li>
<div id="l"><label for="f">Forward:</label></div>
<div id="s"><select name="f">
<?php popselect(Forward); ?>
</select></div>
</li>
<li>
<div id="l"><label for="lm">Left Midfielder:</label></div>
<div id="s"><select name="lm">
<?php popselect("Left Midfielder"); ?>
</select></div>
</li>
<li>
<div id="l"><label for="rm">Right Midfielder:</label></div>
<div id="s"><select name="rm">
<?php popselect("Right Midfielder"); ?>
</select></div>
</li>
<li>
<div id="l"><label for="ld">Left Defender:</label></div>
<div id="s"><select name="ld">
<?php popselect("Left Defender"); ?>
</select></div>
</li>
<li>
<div id="l"><label for="rd">Right Defender:</label></div>
<div id="s"><select name="rd">
<?php popselect("Right Defender"); ?>
</select></div>
</li>
</ul>
</fieldset>
The problem was variable scope...
My function did not have access to $id...
$tquery = "SELECT * FROM teams WHERE `selection_id` = '$_GET['selection_id']'";
worked a charm!

Connect WHERE clause to link

I need to connect a WHERE clause to a link. Here is my code:
<body>
<?php
function toiletPaper($input) {
// Clean input
return $input;
}
$username="USERNAME";
$password="PASSWORD";
$database="DATABASE";
mysql_connect(localhost,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM searchacts";
$result=mysql_query($query);
// process form when posted
$query = "SELECT * FROM searchacts";
$options['PriceLow'] = array("desc" => "Price (Low to High)","query" => " ORDER BY price ASC");
$options['PriceHigh'] = array("desc" => "Price (High to Low)","query" => " ORDER BY price DESC");
$options['NameAZ'] = array("desc" => "Name (A-Z)","query" => " ORDER BY name ASC");
$options['NameZA'] = array("desc" => "Name (Z-A)","query" => " ORDER BY name DESC");
$query = "SELECT * FROM searchacts";
$filter['partybands'] = array("desc" => "Party Bands","query" => " WHERE category='Party Bands'");
// Important: function name is made up
$cleanValue = toiletPaper($_REQUEST['value']);
if (array_key_exists($cleanValue, $options)) {
$query .= $query.$options[$cleanValue];
}
$result = mysql_query($query);
$num = mysql_num_rows($result);
echo "<a href='".$_SERVER['PHP_SELF']."'>All</a>";
foreach ($options as $key => $value) {
echo "<a href='".$_SERVER['PHP_SELF']."?value=".$key."'>".$value['desc']."</a>";
}
?>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='form_filter' >
<select name="value">
<option value="all">All</option>
<option value="PriceLow">Price (Low to High)</option>
<option value="PriceHigh">Price (High to Low)</option>
<option value="NameAZ">Name (A-Z)</option>
<option value="NameZA">Name (Z-A)</option>
</select>
<br />
<input type='submit' value = 'Re-Order'>
</form>
<?php
$i=0;
while ($i < $num) {
$image=mysql_result($result,$i,"image");
$name=mysql_result($result,$i,"name");
$category=mysql_result($result,$i,"category");
$description=mysql_result($result,$i,"description");
$stamps=mysql_result($result,$i,"stamps");
$stickmen=mysql_result($result,$i,"stickmen");
$price=mysql_result($result,$i,"price");
$view=mysql_result($result,$i,"view");
$actpagelink=mysql_result($result,$i,"actpagelink");
?>
<a href="<?php echo $actpagelink; ?>" class="searchitem">
<div class="searchimage"><img src="<?php echo $image; ?>"/></div>
<div class="searchtext">
<div class="searchname"><?php echo $name; ?></div>
<div class="searchcategory"><?php echo $category; ?></div>
<div class="searchdescription"><?php echo $description; ?></div>
</div>
<div class="searchstamps"><img src="<?php echo $stamps; ?>" /></div>
<div class="searchstickmen"><img src="<?php echo $stickmen; ?>" /></div>
<div class="searchprice"><span class="pricefrom">from</span>£<?php echo $price; ?></div>
<div class="searchview"><img src="<?php echo $view; ?>" /></div>
</a>
<?php
$i++;
}
mysql_close();
?>
</body>
So I have my clause:
elseif($_POST['value'] == 'partybands') {
$query = "SELECT * FROM searchacts
WHERE category='Party Bands'";
}
Which I need to connect to a link eg. Party Bands
How can I do that as I will have quite a lot of where clauses to filter the data.
Thanks
Use url parameters and pass in variables, then use those variables to trigger what you want the where clause to say.
I am guessing that you are trying:
http://example.com/yourscript.php?value=partybands
But finding that that does not work...
This is because you are looking for the variable value in the $_POST array.
$_POST['value'] == 'partybands'
If you want to be able to grab the value from either POST or GET you can use $_REQUEST
$_REQUEST['value'] == 'partybands'
You'll also need to modify the topmost conditional isset().
But do take seriously the cautions that others have made here with regards to security and forward compatibility.
Do not trust user data. Must be sanitized. Best way to accomplish is probably through PDO.
I recommend using an associative array and in_array to validate.
Also make sure you sanitize the input before you use it.
EDIT2:
<body>
<?php
function toiletPaper($input) {
// Clean input
return $input;
}
$username="USERNAME";
$password="PASSWORD";
$database="DATABASE";
mysqli_connect("localhost",$username,$password);
#mysqli_select_db($database) or die( "Unable to select database");
// process form when posted
$query = "SELECT * FROM searchacts";
$options['partybands'] = array("desc" => "Party Bands","query" => " WHERE category='Party Bands'");
$options['PriceLow'] = array("desc" => "Price (Low to High)","query" => " ORDER BY price ASC");
$options['PriceHigh'] = array("desc" => "Price (High to Low)","query" => " ORDER BY price DESC");
$options['NameAZ'] = array("desc" => "Name (A-Z)","query" => " ORDER BY name ASC");
$options['NameZA'] = array("desc" => "Name (Z-A)","query" => " ORDER BY name DESC");
// Important: function name is made up
$cleanValue = toiletPaper($_REQUEST['value']);
if (array_key_exists($cleanValue, $options)) {
$query .= $query.$options[$cleanValue];
}
$result = mysqli_query($query);
$num = mysqli_num_rows($result);
echo "<a href='".$_SERVER['PHP_SELF']."'>All</a>";
foreach ($options as $key => $value) {
echo "<a href='".$_SERVER['PHP_SELF']."?value=".$key."'>".$value['desc']."</a>";
}
?>
<br />
<?php
$i=0;
while ($i < $num) {
$image = mysqli_result($result,$i,"image");
$name = mysqli_result($result,$i,"name");
$category = mysqli_result($result,$i,"category");
$description = mysqli_result($result,$i,"description");
$stamps = mysqli_result($result,$i,"stamps");
$stickmen = mysqli_result($result,$i,"stickmen");
$price = mysqli_result($result,$i,"price");
$view = mysqli_result($result,$i,"view");
$actpagelink = mysqli_result($result,$i,"actpagelink");
?>
<a href="<?php echo $actpagelink; ?>" class="searchitem">
<div class="searchimage"><img src="<?php echo $image; ?>"/></div>
<div class="searchtext">
<div class="searchname"><?php echo $name; ?></div>
<div class="searchcategory"><?php echo $category; ?></div>
<div class="searchdescription"><?php echo $description; ?></div>
</div>
<div class="searchstamps"><img src="<?php echo $stamps; ?>" /></div>
<div class="searchstickmen"><img src="<?php echo $stickmen; ?>" /></div>
<div class="searchprice"><span class="pricefrom">from</span>£<?php echo $price; ?></div>
<div class="searchview"><img src="<?php echo $view; ?>" /></div>
</a>
<?php
$i++;
}
mysql_close();
?>

Categories