TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Never ending increment? (http://www.talkphp.com/general/2245-never-ending-increment.html)

Orc 02-13-2008 10:43 PM

Never ending increment?
 
I probably sound like a noob for this cause I never tried it. :P
Usually when incrementing, it only increments by one, but I want it to keep incrementing.

PHP Code:

$foo 0;
$foo++; 

This only increments it by 1 and then stops, and never increments again, now I might just have to make an existing counter or something to increment but this is for counting how many views are from a certain id in a database. Help, do I make this an Array, and keep adding onto the array by counting then adding that into it? :S

Village Idiot 02-13-2008 11:33 PM

Im not sure what you are trying to do, if you want to incriment a variable by more then one
PHP Code:

$var += 10


Rendair 02-13-2008 11:35 PM

can you do some kind of select

PHP Code:

$query mysql_query("SELECT * FROM example WHERE id = '$id'") or die(mysql_error());
$num mysql_num_rows($query); 

Or don't i understand what you actually mean by this question? maybe someone can explain?

Orc 02-13-2008 11:41 PM

Quote:

Originally Posted by Village Idiot (Post 10703)
Im not sure what you are trying to do, if you want to incriment a variable by more then one
PHP Code:

$var += 10


I've never used that operator.

Also, when a user clicks on say an id on a page, then it increments the views column by one, and is displayed on the page.

Village Idiot 02-13-2008 11:54 PM

I dont think there is a way to do that.

Orc 02-13-2008 11:56 PM

Quote:

Originally Posted by Village Idiot (Post 10709)
I dont think there is a way to do that.

There has to be a way. I've seen it before so..

Rendair 02-14-2008 12:07 AM

You can more likely do it using Ajax seems simply enough

Alan @ CIT 02-14-2008 12:55 AM

I'm not entirely sure what you are trying to do, but in your original code, the reason it only increments it to 1 is because you are resetting the variable to 0 before you increment it.

I'm guessing that you want to do something like vBulletins thread views counter. The general idea is to get the number of views (ie, from a database), increment it by one, then store the new value back in the database.

If you wanted to update the page instantly, then use Ajax as Rendair suggested after you have read/write the values to the database.

Alan

Village Idiot 02-14-2008 03:06 AM

What are you attempting to do? Reading your post again, it really doesnt make any sense.

Orc 02-14-2008 03:09 AM

I'm trying to increment every time there is a new view, a view is when someone goes onto a special page, and then it inserts the view into the mysql, and shows it on the front page. This is what I want for my image uploading script.

SOCK 02-14-2008 03:49 AM

Code:

UPDATE table
SET view_count= view_count+1
WHERE pageID= 1;


Wildhoney 02-14-2008 04:06 AM

Quote:

Originally Posted by Orc (Post 10705)
I've never used that operator.

Also, when a user clicks on say an id on a page, then it increments the views column by one, and is displayed on the page.

It's not really a single operator, but rather a mixture of 2. PHP has a lot of them. It allows you to omit repeating the variable again. Otherwise it'd be like:

php Code:
$i = $i + 1;

So instead we use the following:

php Code:
$i /= $i;
$i += $i;
$i -= $i;
$i *= $i;

Orc 02-14-2008 06:44 AM

Quote:

Originally Posted by SOCK (Post 10737)
Code:

UPDATE table
SET view_count= view_count+1
WHERE pageID= 1;


You just solved my problem, but it increments by 2 every time I view a page.

ReSpawN 02-14-2008 09:33 AM

PHP Code:

$postQuery mysql_query('SELECT * FROM post WHERE id = "'.$_GET['id'].'"');
$postFetch mysql_fetch_array($postQuery);

$newViews $postFetch['viewcount'] + 1;

mysql_query('UPDATE post SET viewcount = "'.$newViews.'" WHERE id = "'.$_GET['id'].'"'); 

Finito.

Rendair 02-14-2008 10:30 AM

You can make it update the page instantly without having to refresh using something like this. You create 2 pages on with that script ReSpawn said or whatever and then the main page where you have some kind of onClick or something.

PHP Code:

<script type="text/javascript">
//<![CDATA[
var request false;

if(
window.XMLHttpRequest){
  
request = new XMLHttpRequest();
}else if(
window.ActiveXObject){
  
request = new ActiveXObject("Microsoft.XMLHTTP");
}

function 
getData(source){
  if(
request){

    var 
sourceId document.getElementById("hiddenFieldId").value;
    var 
newsource source "?id=" sourceId;
    
request.open("GET",newsource);
    
request.onreadystatechange = function(){
      
         if(
request.readyState 4){
              
document.getElementById("totalviews").innerHTML "Updating....";
         }
         else if(
request.readyState==4){
             
document.getElementById("totalviews").innerHTML request.responseText;
          }
          
    }
    
request.send(null);
  }
}

//]]>
</script> 

To use the script you can use something like

PHP Code:


<a href="#" onClick="getSource('process_views.php');"><div id="totalviews"4 Views </div></a

Or something along that line anyway.

This example has a use of a hidden field containing the id then you can use the MySQL on a new page.

PHP Code:

$postQuery mysql_query('SELECT * FROM post WHERE id = "'.$_GET['id'].'"');
$postFetch mysql_fetch_array($postQuery);

$newViews $postFetch['viewcount'] + 1;

mysql_query('UPDATE post SET viewcount = "'.$newViews.'" WHERE id = "'.$_GET['id'].'"'); 

echo 
$numViews// echo the new number of views to be displayed 


ReSpawN 02-14-2008 10:47 AM

Yeah, then you're using the AJAX framework. There are a lot of plugins for it if you're into it. Check out mootools.net Orc. :)

Orc 02-14-2008 10:49 AM

It keeps saying on Gmail, that Village Idiot replied to this thread. :/

ReSpawN 02-14-2008 01:23 PM

What do you mean? If you mean it gets the same ID over and over, you should make the $_GET['post'] or what ever dynamic.

Orc 02-14-2008 10:59 PM

Quote:

Originally Posted by ReSpawN (Post 10749)
What do you mean? If you mean it gets the same ID over and over, you should make the $_GET['post'] or what ever dynamic.

Eh? No I ment that Gmail is screwed up, it keeps showing that Village Idiot has replied to this thread, but it's only someone else. :/

Village Idiot 02-14-2008 11:41 PM

Orc, if you want us to help you, you need to help us. What are you trying to do? We dont understand what you have previously said. If we know what you are trying to accomplish we could probably tell you how to do it.


All times are GMT. The time now is 12:44 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0