mysql Simple Query + if - php

Seems like I'm not able to do a simple query.
What I want to achieve is all the results that have 'best2' value '1' so the div will be attached to only those results.
Right now , the div 'badge-best2div' attaches to ALL the products.
I don't know what I'm missing out.
<?php $show = true; ?>
<?php
$roman = Doctrine_Query::create()->from("Product")
->where("status = 1")
->andWhere("site_id = ? ", SITE_ID)
->andWhere("best2 = 1")
->fetchOne(array(), Doctrine_Core::HYDRATE_ARRAY);
?>
<?php if ($product->getIsTop() && !isset($hide_badge_top)): $show = false;?>
<div class="badge-top"></div>
<?php endif; ?>
<?php if ($roman['best2']){
$show = false; ?>
<div class="badge-best2div"></div>
<?php } ?>
<?php if ($product->getIsBestBuy() && !isset($hide_badge_bestbuy) && $show): ?>
<div class="badge-bestbuy"></div>
<?php endif ; ?>

got it working using
<?php if (($roman['best2'])==1){

Related

PHP How to add active class to dynamic menu?

how can i add active class to dynamic menu.I use while for my product list.But i cannot understand how can i add to active class.
<div class="col-md-4">
<h3>Products</h3>
<div class="list-group">
<?php
$db = new connection();
$productsor = mysqli_query($db,"select * from products");
while($productcek = mysqli_fetch_assoc($productsor)){ ?>
<?php echo $productcek['products_ad']; ?>
<?php
}
?>
</div>
</div>
You need to check against a previously set _GET parameter and compare it in your while loop
while($productcek = mysqli_fetch_assoc($productsor)){ ?>
<?php $active = (isset($_GET['products_id']) && $productcek['products_id'] == $_GET['products_id']) ? 'active' : null; ?>
<?php echo $productcek['products_ad']; ?>
<?php
}
?>

Foreach for database

So I have the following code:
<?php
$post_id = 1;
$name_result = $wpdb->get_results("SELECT * FROM rh_names WHERE post_id = '$post_id' ");
?>
<?php if (!empty($name_result)) { ?>
<?php foreach ($name_result as $names) {?>
<div class="names">
<script type="text/javascript">
var nameXML = <?php echo json_encode($names); ?>;
document.write(nameXML.last_name);
</script>
</div>
<?php } ?>
<?php } ?>
In the database, I have two results that should show up, but I am only getting one.
Why am I only getting one result when two should show up? What am I missing?
Thanks!
document.write will replace all document element so you can see last result only try
document.getElementById('results').innerHTML = res_content;

Show Ads In Middle Of Results

I'm currently using sphider on one of my websites, my questions is how can I break the results page into 2 parts to add a 200px break to place a ad slot.
Code:
<?php
extract($search_results);
?>
<?php if ($search_results['did_you_mean']){?>
<div id="did_you_mean">
<?php echo $sph_messages['DidYouMean'];?>: <?php print $search_results['did_you_mean_b']; ?>?
</div>
<?php }?>
<?php if ($search_results['ignore_words']){?>
<div id="common_report">
<?php while ($thisword=each($ignore_words)) {
$ignored .= " ".$thisword[1];
}
$msg = str_replace ('%ignored_words', $ignored, $sph_messages["ignoredWords"]);
echo $msg; ?>
</div>
<?php }?>
<?php if ($search_results['total_results']==0){?>
<div id ="result_report">
<?php
$msg = str_replace ('%query', $ent_query, $sph_messages["noMatch"]);
echo $msg;
?>
</div>
<?php }?>
<?php if ($total_results != 0 && $from <= $to){?>
<div id ="result_report">
<?php
$result = $sph_messages['Results'];
$result = str_replace ('%from', $from, $result);
$result = str_replace ('%to', $to, $result);
$result = str_replace ('%all', $total_results, $result);
$matchword = $sph_messages["matches"];
if ($total_results== 1) {
$matchword= $sph_messages["match"];
} else {
$matchword= $sph_messages["matches"];
}
$result = str_replace ('%matchword', $matchword, $result);
$result = str_replace ('%secs', $time, $result);
echo $result;
?>
</div>
<?php }?>
<?php if (isset($qry_results)) {
?>
<div id="results">
<!-- results listing -->
<?php foreach ($qry_results as $_key => $_row){
$last_domain = $domain_name;
extract($_row);
if ($show_query_scores == 0) {
$weight = '';
} else {
$weight = "[$weight%]";
}
?>
<?php if ($domain_name==$last_domain && $merge_site_results == 1 && $domain == "") {?>
<div class="idented">
<?php }?>
<b><?php print $num?>.</b> <?php print $weight?>
<?php print ($title?$title:$sph_messages['Untitled'])?><br/>
<div class="description"><?php print $fulltxt?></div>
<div class="url"><?php print $url2?> - <?php print $page_size?></div>
<?php if ($domain_name==$last_domain && $merge_site_results == 1 && $domain == "") {?>
[ More results from <?php print $domain_name?> ]
</div class="idented">
<?php }?>
<br/>
<?php }?>
</div>
<?php }?>
<!-- links to other result pages-->
<?php if (isset($other_pages)) {
if ($adv==1) {
$adv_qry = "&adv=1";
}
if ($type != "") {
$type_qry = "&type=$type";
}
?>
<div id="other_pages">
<?php print $sph_messages["Result page"]?>:
<?php if ($start >1){?>
<?php print $sph_messages['Previous']?>
<?php }?>
<?php foreach ($other_pages as $page_num) {
if ($page_num !=$start){?>
<?php print $page_num?>
<?php } else {?>
<b><?php print $page_num?></b>
<?php }?>
<?php }?>
<?php if ($next <= $pages){?>
<?php print $sph_messages['Next']?>
<?php }?>
</div>
<?php }?>
<div class="divline">
</div>
I'm also not aware of a live PHP code editor, if you know of one please comment and share so I can add a link!
Presuming $from and $to are the result numbers, so you're displaying "Showing results 10 to 30 of 100" for example:
<div id="results">
<!-- results listing -->
<?php $adbreak = ($to - $from) / 2;
<?php foreach ($qry_results as $_key => $_row){
<?php if ($adbreak == 0) { ?>
<div id="results-adbreak">
<img src="buy-a-car.jpg" alt="one careful owner!" />
</div>
<?php }
$adbreak--;
?>
// rest of your code
This will put a div approximately (give or take one) half way down your page of results. You can obviously replace the ad with a call to whatever you want.
adding something like:
<?php $adbreak = ($to - $from) / 2;
<?php if ($adbreak < 5) $adbreak = -1; ?>
will ensure that it doesn't display at all if the results list is too short.
If you don't know $to and $from in advance, you can still do it, but you'll have to calculate the equivalent from the query result first.

Using PHP loop to reduce code

I have few drop down boxes from where I can get an id of a category. For example, from drop down box 1, i get $cat1, from box 2 i get $cat2 and so on.
Then I want to get the entries from db for each of the cat id. Currently I am repeating same code for each of the variable, like:
<?
$cat1 = 1;
$cat2 = 3;
$cat3 = 4;
$cat4 = 8;
<? if ($var1 != ""){ ?>
<div>
Entries for <? echo $var1; ?>
..
</div>
<? } ?>
<? if ($var2 != ""){ ?>
<div>
Entries for <? echo $var2; ?>
..
</div>
<? } ?>
<? if ($var3 != ""){ ?>
<div>
Entries for <? echo $var3; ?>
..
</div>
<? } ?>
I'd like to know if I can use a loop and avoid writing code for each variable.
Try
<?
$cats = array(1,3,4,8);
foreach($cats as $value) {
if($value != "") {
?>
<div>Entries for <?= $value; ?></div>
<?
}
}
?>
Use an array like this:
$cat[1] = 'bla';
$cat[2] = 'Bla2';
foreach ($cat as $c){
if ($c != ""){
echo '
<div>
Entries for '. $c.'
..
</div>';
}
}

processing list of items in sections

I want 3 item per div (the sample has a total of 7 items) like this:
<div>
<item/>
<item/>
<item/>
</div>
<div>
<item/>
<item/>
</div>
But I can't do
while($r = mysql_fetch_array($q){
?><item/><?
}
if(++$i%3==1){
//blabla:)
}
Because it incorrectly prints out
<div>
<item/>
<item/>
<item/>
</div>
<div>
<item/>
...
How do I correctly print out the items in blocks of 3?
You were most of the way there.
$result = mysql_query("SELECT ....");
$recordCounter = 0;
$record = mysql_fetch_assoc($result);
while ($record) {
if ($recordCounter % 3 == 0) {
?>
<div class="item-block">
<?php
}
?>
<div class="item"></div>
<?php
$record = mysql_fetch_assoc($result);
$recordCounter++;
if ($record === false || $recordCounter % 3 == 0) {
?>
</div>
<?php
}
}
mysql_free_result($result);
somewhat shortened :))
<div class="items">
<? $q = mysql_query("SELECT * FROM table ");
$i=1;
while($r = mysql_fetch_array($q))
{
?><item /><?
if($i%4==0){?></div><div class="item"><? }?>
<? $i++;
}?>
</div>
Here is another option, maybe there are too much php tags =)
<?php
$items = array(1,2,3,4,5,6,7,8);
$count = 1;
?>
<html>
<body>
<?php foreach ($items as $item) : ?>
<?php if ($count == 1) : ?>
<div>
<?php endif; ?>
<p><?php echo $item; ?></p>
<?php if ($count == 3) : ?>
</div>
<?php $count = 0; ?>
<?php endif; ?>
<?php $count++; ?>
<?php endforeach; ?>
</body>
</html>

Categories