PHP hits counter - php

Im having a problem with a php script. I want to make it work so when under 5 hits it shows a locked button and greater than 5 hits it shows a unlocked button.
<?
if($hits < 5){
?>
<div id="status-button-locked"></div>
<?
}
?>
<?
if($hits > 5){
?>
<div id="status-button-unlocked"></div>
<?
}
?>
I have tried the code above but it didn't work, when it is greater than 5 it just wont show anything.

It won't show anything, when 5 hits exactly. Try:
<?
if($hits < 5){
?>
<div id="status-button-locked"></div>
<?
} else {
?>
<div id="status-button-unlocked"></div>
<?
}
?>

Like David said it now doesn't react when it hits exactly 5.
Try this:
<?php
if($hits >= 5){ // this part happens if $hits is 5 or higher.
?>
<div id="status-button-unlocked"></div>
<?php
} else {
?>
<div id="status-button-locked"></div>
<?php
}
?>
this way you handle errors like when $hits is empty.

Related

Detect Category ID=4 in Joomla and show div

I would like to detect a certain category with ID=4 and then apply if/else to add div to display contents.
<?php
if (JRequest::getVar('view')=='categories' && JRequest::getVar('id')==4) { ?>
<?php } ?>
How do I achieve this?
I want to show this if the article belongs to category 4 else show another div.
Imagining this two be the different div
<div class="category4">text here</div>
<div class="categoryxxx">text here</div>
Do note that <?php echo $this->item->catid;?> shows the correct Category ID. I would like to apply if and else statement using catid==9 or something. Just not good at PHP.
You can also put directly the html code, avoiding echo. It may useful especially when html code is sizeable.
<?php if ($this->item->catid == 4): ?>
<div class="category4">text here</div>
<!--- any other html code --->
<?php else: ?>
<div class="categoryxxx">text here</div>
<!--- any other html code --->
<?php endif; ?>
Hey I got it working :)
<?php
if ($this->item->catid == 9) {
echo "yes, it exists";
}
else {
echo "no, it don't exist";
}
?>

How do I excluding INDEX or Home page in PHP Code?

I know nothing about PHP. I have the code below to show next and prev at the bottom of each page, with <-|-> as a separator but I don't want it on the home/index page.
<div align="center" >
<?php echo previous_page_not_post(); ?> <-|-> <?php echo next_page_not_post(); ?>
</div>
How can I do this?
You can add an if statement like this:
<div align="center">
<?php
if (basename($_SERVER['PHP_SELF']) != "index.php") {
echo previous_page_not_post()." <-|-> ".next_page_not_post();
}
?>
</div>
(Look at get current php page name)

Data-binding and php error

Hi this is what I'm trying to do.
What I'm trying to do is if the data-bind id() is equal to 1 or 2 hide it , but show all the others.
<?php if($authUser->Plan=='3'){ ?>
<div class="image-item" data-bind="visible: id() == 1,2, css:{hide}" data-bind="css:{active: ($parent.theme()==id())}">
<?php } ?>
Thank you.
<?php if($authUser->Plan=='3'){ ?>
<div class="image-item" data-bind="visible: ('simple'==id()) || ('amelia'==id()), css:{active: ($parent.theme()==id())}">
<?php } ?>

How to return a specific row in PHP and MySQL?

I am trying to add a mouseover hover which basically works on the first MySQL returned query. However it will not work. It does not recognise my IF ELSE statement and it just returns the ELSE command.
My data is like
gallery
----------------------
id sku img types
1 454_red front.jpg F
2 454_red back.jpg F
3 452_red front.jpg F
4 452_red back.jpg F
5 452_red a1.jpg S
6 452_red a2.jpg S
My PHP
<?
$imgsql=mysql_query("SELECT * FROM `gallery` WHERE `gallery`.`sku` = '".$r['sku']."' ORDER BY `gallery`.`type` ASC");
while($rimg=mysql_fetch_array($imgsql)){
?>
<? if($rimg == $rimg['0']){ ?>
<div>
<img src="//super.cdn.com/<?=$r['sku']?>/<?=$rimg['img']?>.jpg" onmouseover="this.src='//super.cdn.com/<?=$r['sku']?>/back.jpg'" onmouseout="this.src='//super.cdn.com/<?=$r['sku']?>/<?=$rimg['img']?>.jpg'"/>
</div>
<? } else { ?>
<div>
<img src="//super.cdn.com/<?=$r['sku']?>/<?=$rimg['img']?>.jpg"/>
</div>
<? } ?>
<? } ?>
The $r["sku"] is called at the top of the code, this sits inside a product listing loop.
To make it simple, then take another variable say counter or flag as below
<?
$count = 1;
$imgsql=mysql_query("SELECT * FROM `gallery` WHERE `gallery`.`sku` = '".$r['sku']."' ORDER BY `gallery`.`type` ASC");
while($rimg=mysql_fetch_array($imgsql)){
?>
<? if($count==1){ ?>
<div>
<img src="//super.cdn.com/<?=$r['sku']?>/<?=$rimg['img']?>.jpg" onmouseover="this.src='//super.cdn.com/<?=$r['sku']?>/back.jpg'" onmouseout="this.src='//super.cdn.com/<?=$r['sku']?>/<?=$rimg['img']?>.jpg'"/>
</div>
<? } else { ?>
<div>
<img src="//super.cdn.com/<?=$r['sku']?>/<?=$rimg['img']?>.jpg"/>
</div>
<? } ?>
<?
$count = 0;
}
?>

php + Count loops + Wrong initial count?

I have a rather long code, so i think the best is I post it here with some additional comments as questions inside of it:
<div class="contentRow">
<h2>Title</h2>
<div id="frame">
<?php
//Get files
$sql->query("SELECT * FROM files WHERE job = 'true'");
$count = count($sql->get());
//No files to display
if ($count == "0"){
echo "<div class='box red'><p>No jobs found.</p></div>";
} else{
$sql->query("SELECT * FROM files WHERE job = 'true'");
$i=0;
foreach($sql->get() as $result){
$i++;
?>
<!-- Here I start printing the results and here the problems begin -->
<!-- My goal here: Print n .jobsContainer with exactly 4 .jobContainer inside -->
<?php if ($i % 4 == 0){ ?>
</div><!-- CLOSE jobsContainer -->
<?php } ?>
<?php if ($i % 4 == 0 OR $i == 1){ ?>
<div class="jobsContainer"><!-- OPEN jobsContainer -->
<?php } ?>
<!-- Print the .jobContainer -->
<div class="jobContainer">
content + table + form
</div>
<!-- My goal here: Print .jobSeperator after every 2nd .jobContainer -->
<?php if ($i % 2 == 0){ ?>
<div class="jobSeperator"> </div>
<?php } ?>
<!-- CLOSE loop -->
<?php } }?>
</div><!-- jobsFrame -->
<div class="clear"></div>
</div><!-- contentRow -->
The desired output should look like this:
My problem is following:
It seems that there are only 3 elements included in the first container, but after that there are 4. Therefore also the seperators are kinda messed up and it looks like this:
It seems that the problem is not applying to the .jobSeperator, as it does always get included after every 2th container, however it gets messed up too, because of the fact that in the first .jobsContainer there are just 3 sub containers.
I dont get why in the first .jobsContainer there are just 3 sub containers, but from than on there are, as desired, 4, but probably it is obvious and I just dont see it...
p.s. If I set the code to print 5 .jobContainer inside one .jobsContainer, than there are printed 4 in the first .jobsContainer and from than on 5 in every following .jobsContainer
First of all, you should not keep your SQL logic mixed with HTML.
Anyway. The idea is that you need to output a separator after each second item. The testcase i would look at would be like this.
1 2
---
3 4
***
5 6
---
7
And the code for writing it would be:
$i = 0;
foreach( /* some array */ as $data ){
$i++;
if ( ($i - 1) % 2 === 0 ){
if ( ($i - 1) % 4 === 0 ){
echo '<br />***<br />';
} else {
echo '<br />---<br />';
}
}
echo $i, ' ';
}
This would reproduce the structure above (never tested).
As for the separation of layout and rest of application logic, you might look in to this article. It will show a quite simple way, without a need of additional 3rd party libraries.
try to change these lines:
<?php if ($i % 4 == 0 OR $i == 1){ ?>
<div class="jobsContainer"><!-- OPEN jobsContainer -->
<?php } ?>
to
<?php if ( ($i-1) % 4 == 0 ){ ?>
<div class="jobsContainer"><!-- OPEN jobsContainer -->
<?php } ?>
Now you open container at the begginning of 1st, 4th, 8th, 12th,... iteration, after this change you should open container before 1st, 5th, 9th, 13th,... iteration which should fix the problem. And you will get rid of ugly or. It seems this is the only bug, just try it.
I also suggest separating application logic and presentation.
I believe You're making one small mistake.
<!-- My goal here: Print n .jobsContainer with exactly 4 .jobContainer inside -->
<?php if ( ($i+1) % 4 == 0){ ?>
</div><!-- CLOSE jobsContainer -->
<?php } ?>
<?php if ( ($i+1) % 4 == 0 OR $i == 1){ ?>
<div class="jobsContainer"><!-- OPEN jobsContainer -->
<?php } ?>
Use ($i+1) instead of $i in this bit.
If you inspect the generated HTML output, you'll see that when $i == 4, you first close the jobsContainer div before outputting the 4th jobContainer. You need to change the order of output to something like this:
EDIT: seems like you should split up the start of the first jobsContainer ($i == 1)
<div class="contentRow">
<h2>Title</h2>
<div id="frame">
<?php
$i = 0;
for($j = 0; $j < 12; $j++)
{
$i++;
?>
<?php if ($i == 1) { ?>
<div class="jobsContainer">
<?php } ?>
<div class="jobContainer">job</div>
<?php if ($i % 4 == 2) { ?>
<div class="jobSeperator"> </div>
<?php } ?>
<?php if ($i % 4 == 0) { ?>
</div>
<div class="jobsContainer">
<?php } ?>
<?php
}
?>
<?php if ($i > 0) { ?>
</div>
<?php } ?>
</div>
<div class="clear"></div>
</div>

Categories