PDA

View Full Version : سوال: کمک برای ایجاد شمارنده برای هر پست ( جداگانه )



mamali-mohammad
جمعه 19 فروردین 1390, 18:47 عصر
سلام
دوستان با توجه به تاپیک زیر تونستم بازدید کل رو بسازم :

http://barnamenevis.org/showthread.php?269921-ساخت-باکس-تعداد-بازدید-کنندگان-سایت

حالا مشکلم برای بازدید از صفحه هست
فرض کنید هر ID باید بازدید خودش رو داشته باشه

مثلا بازدید مطلب 8 جدا هست و بازدید مطلب 9 جدا ( از این طریق میخوام مطالب پر بازدید رو هم بزارم )
حالا این تیبل ما شامل اینا میشه :
ID , Title , Price , Counter

که میخوام با هر بازدید یدونه بهش اضافه بشه
ممنون میشم کمک کنید

اینم کد بنده برای نمایش هر صفحه :


if (isset($_GET['id'])){
$ir=$_GET['id'];
$sql=mysql_query("SELECT * FROM mybanner WHERE id='$ir'");
while ($row=mysql_fetch_array($sql)){
$title=$row["title"];
$id=$row["id"];
$price=$row["price"];
$counter=$row["counter"];
}
}
?>

ایدی ها در صفحه test.php باز میشه .
اینطوری :


http://127.0.0.1/my/test.php?id=6

ممنون میشم کمک کنید

Mr.Moghadam
جمعه 19 فروردین 1390, 19:15 عصر
هر بار که صفحه مربوط به هر مطلب باز شد اول مقدار شمارنده رو بخون بعد یه دونه به مقدارش اضافه کن و سپس دیتابیس رو آپدیت کن

binyaft
جمعه 19 فروردین 1390, 19:18 عصر
update `posts` set `counter`=counter+1 where `id`='$id'

mamali-mohammad
شنبه 20 فروردین 1390, 00:10 صبح
آقا نوشتم ، نشد !
اینم کد


<?php
include "config/connect_to_mysql.php";
include "jdf.php";
if (isset($_GET['id'])){
$ir=$_GET['id'];
$sql=mysql_query("SELECT * FROM send WHERE id='$ir'");
$row=mysql_fetch_array($sql);
$title=$row["title"];
$id=$row["id"];
$price=$row["price"];
$total=$row['total'];
$yesterday=$row['yesterday'];
$today=$row['today'];
$date=$row['date'];
@list($year,$month,$day)=split('-',$date);
$CurrentDate=date("Y-m-d");
@list($CurrentYear,$CurrentMonth,$CurrentDay) = split ('-',$CurrentDate);
function Update($today,$yesterday,$total,$date){
mysql_query("UPDATE send SET today='$today' WHERE id='$ir'");
}
$total++;
if($CurrentYear==$year){
if($CurrentMonth==$month){
if($CurrentDay==$day){
$today++;
Update($today,$yesterday,$total,$CurrentDate);
}else{
if($CurrentDay==++$day){
$yesterday=$today;
$today=1;
Update($today,$yesterday,$total,$CurrentDate);
}else{
$yesterday=0;
$today=1;
Update($today,$yesterday,$total,$CurrentDate);
}
}
}else{
if($CurrentMonth==++$month){
$yesterday=$today;
$today=1;
Update($today,$yesterday,$total,$CurrentDate);
}else{
$yesterday=0;
$today=1;
Update($today,$yesterday,$total,$CurrentDate);
}
}
}
else{
if($CurrentYear==++$year){
$yesterday=$today;
$today=1;
Update($today,$yesterday,$total,$CurrentDate);
}
else{
$yesterday=0;
$today=1;
Update($today,$yesterday,$total,$CurrentDate);

}

}}
?>

اینم جواب نداد :

mysql_query("UPDATE send SET today='$today+1',yesterday='$yesterday+1',total='$ total+l' ,date='$date' WHERE id='$ir'");

mtchabok
شنبه 20 فروردین 1390, 09:24 صبح
عزیزم در کد دومی که گذاشتید به این صورت صحیحش هس :

mysql_query("UPDATE send SET `today`=`'$today' ,yesterday='$yesterday' ,total='$total' ,date='$date' WHERE id='$ir'");

mamali-mohammad
شنبه 20 فروردین 1390, 12:20 عصر
کد اولی رو که گذاشتم چک کنید
دقیقا همین هست
اما جواب نمیده

الان اگه فقط به یه بدم ( id=1 ) کار می کنه
اما برای هر id کار نمی کنه

mtchabok
شنبه 20 فروردین 1390, 18:57 عصر
عزیزم اینرو یه تستی کنید ببنید جواب درستی میده :

<?php
include "config/connect_to_mysql.php";
include "jdf.php";
if (isset($_GET['id']) and !empty($_GET['id'])){
$ir=$_GET['id'];
$sql=mysql_query("SELECT * FROM send WHERE id='$ir' limit 1");
$row=mysql_fetch_assoc($sql);
$title=$row["title"];
$id=$row["id"];
$price=$row["price"];
$total=$row['total'];
$yesterday=$row['yesterday'];
$today=$row['today'];
$date=$row['date'];
@list($year,$month,$day)=split('-',$date);
$CurrentDate=date("Y-m-d");
@list($CurrentYear,$CurrentMonth,$CurrentDay) = split ('-',$CurrentDate);
$total++;
if($CurrentYear==$year and $CurrentMonth==$month and $CurrentDay==$day){
$today++;
}elseif($CurrentYear==$year and $CurrentMonth==$month and $CurrentDay==++$day){
$yesterday=$today;
$today=1;
}elseif($CurrentYear==$year and $CurrentMonth==$month){
$yesterday=0;
$today=1;
}elseif($CurrentYear==$year and $CurrentMonth==++$month){
$yesterday=$today;
$today=1;
}elseif($CurrentYear==$year){
$yesterday=0;
$today=1;
}elseif($CurrentYear==++$year){
$yesterday=$today;
$today=1;
}else{
$yesterday=0;
$today=1;
}

mysql_query("UPDATE send SET `today`='$today', `yesterday`='$yesterday', `total`='$total' ,
`data`='".date('Y-m-d',$data)."' WHERE id='$ir'");

}
?>

mamali-mohammad
شنبه 20 فروردین 1390, 22:17 عصر
نشد
نمیدونم چیکار کنم دیگه :ناراحت:

mtchabok
یک شنبه 21 فروردین 1390, 00:59 صبح
اول اینکه مطلب برات نمایش داده میشه ؟
دوم اینکه کجاش انجام نمیشه ؟
سوم اینکه خط به خط die بزن و یکی یکی از متغیرها رو درش قرار بده و ببین از لحاظ منطقی کجا ایراد داره . کجا دچار مشکل میشه .

mamali-mohammad
یک شنبه 21 فروردین 1390, 14:40 عصر
مقدار رو نشون میده ( پیش فرض 0 یا هر عددی که بزاریم )
با هر بار رفرش ، مقدار رو زیاد نمیکنه
در صورتی که در حالت عادی ، مقدار رو زیاد میکنه

mamali-mohammad
یک شنبه 21 فروردین 1390, 21:46 عصر
دوستان میتونید کمک کنید ؟
ممنون

mtchabok
دوشنبه 22 فروردین 1390, 10:00 صبح
عزیزم واقعا ببخشید ... کدهای قبلی که نوشتم رو اصلا از لحاظ منطقی نگاه نکرده بودم که برات گذاشتم .
ولی این کدها رو ببین ... این دیگه درست جواب میده :

<?php
include "config/connect_to_mysql.php";
include "jdf.php";
if (isset($_GET['id']) and is_numeric($_GET['id'])){
$ir=(int)$_GET['id'];
$sql=mysql_query("SELECT * FROM send WHERE id='$ir' limit 1");
$row=mysql_fetch_assoc($sql);
extract($row); // var $title, $id, $price, $total, $yesterday, $today, $date
var $year,$month,$day;
@list($year,$month,$day)=split('-',$date);
$year = (int) $year; $month = (int) $month; $day = (int) $day;
$CurrentDate=date("Y-m-d");
@list($CurrentYear,$CurrentMonth,$CurrentDay) = split ('-',$CurrentDate);
$total++;
if($CurrentYear==$year and $CurrentMonth==$month and $CurrentDay==$day){
$today++;
}elseif($CurrentYear==$year and $CurrentMonth==$month){
$yesterday = ($CurrentDay==($day+1))?$today:0;
$today=1;
}elseif($CurrentYear==$year){
$yesterday = ($CurrentMonth==($month+1))?$today:0;
$today=1;
}else{
$yesterday = ($CurrentYear==($year+1))?$today:0;
$today=1;
}

mysql_query("UPDATE send SET `today`='$today', `yesterday`='$yesterday', `total`='$total' ,
`data`='".date('Y-m-d')."' WHERE id='$ir'");

}
?>

mamali-mohammad
دوشنبه 22 فروردین 1390, 11:02 صبح
ممنون
روی این خط ارور میده :

var $year,$month,$day;


Parse error: syntax error, unexpected T_VAR in C:\Program Files\EasyPHP-5.3.3\www\tyy\test.php on line 12

mtchabok
دوشنبه 22 فروردین 1390, 11:51 صبح
اون خط رو میتونی حذفش کنی .

mamali-mohammad
دوشنبه 22 فروردین 1390, 12:12 عصر
کار نمیکنه :ناراحت:
مقدارش رو نشون میده
با هر بار رفرش اضافه نکنه ولی :ناراحت:

mtchabok
دوشنبه 22 فروردین 1390, 12:40 عصر
مطمئنی که اطلاعات از دیتابیس برات میاد .
در ضمن مشخصات جدولت رو واسم بزار .
یه چیز دیگه هم اینکه متغیر لینک به mysql رو هم برام بنویس .
تا بعدش به صورت کامل واست درستش کنم .

mamali-mohammad
دوشنبه 22 فروردین 1390, 12:56 عصر
آره مطمئنم
چون اگه id=$ir نزارم با هر بار رفرش ، اضافه میکنه

mamali-mohammad
سه شنبه 23 فروردین 1390, 13:49 عصر
دوستان ؟
راهی ؟ حلی ؟
ممنون

mtchabok
سه شنبه 23 فروردین 1390, 20:43 عصر
ببخشید که اینقدر دیر دارم جواب میدم ...

<?php
include "config/connect_to_mysql.php";
include "jdf.php";
if (isset($_GET['id']) and is_numeric($_GET['id'])){
$ir=(int)$_GET['id'];
$sql=mysql_query("SELECT * FROM `send` WHERE `id`='$ir' limit 1");
$row=mysql_fetch_assoc($sql);
extract($row); // var $title, $id, $price, $total, $yesterday, $today, $date
@list($year,$month,$day)=split('-',$date);
$year = (int) $year; $month = (int) $month; $day = (int) $day;
$CurrentDate=date("Y-m-d");
@list($CurrentYear,$CurrentMonth,$CurrentDay) = split ('-',$CurrentDate);
$CurrentYear = (int) $CurrentYear; $CurrentMonth = (int) $CurrentMonth;
$CurrentDay = (int) $CurrentDay;
$total++;
if($CurrentYear==$year and $CurrentMonth==$month and $CurrentDay==$day){
$today++;
}elseif($CurrentYear==$year and $CurrentMonth==$month){
$yesterday = ($CurrentDay==($day+1))?$today:0;
$today=1;
}elseif($CurrentYear==$year){
$yesterday = ($CurrentMonth==($month+1))?$today:0;
$today=1;
}else{
$yesterday = ($CurrentYear==($year+1))?$today:0;
$today=1;
}

if(! mysql_query("update `send` set `today`='$today', `yesterday`='$yesterday', `total`='$total' ,
`date`='".date('Y-m-d')."' where `id`='$ir'")) die('save data failed .');

}
?>

mamali-mohammad
سه شنبه 23 فروردین 1390, 21:39 عصر
مرسی عزیزم
اوکی شد
دمت گرم
مشکل کجا بود ؟

mtchabok
چهارشنبه 24 فروردین 1390, 09:44 صبح
این خط رو با قبلیها مقایسه کن .

mysql_query("update `send` set `today`='$today', `yesterday`='$yesterday', `total`='$total' , `date`='".date('Y-m-d')."' where `id`='$ir'")

از این به بعد برای نام های جدولهات و یا فیلدهای جدولهات از کاراکتر ` استفاده کن .
مثلا وقتی می خوای بنویسی جدول send بنویس `send` و یا وقتی می خوای بنویسی فیلد id بنویس `id` . با همین تغییر ساده خیلی از مشکلاتت حل میشه .
این به خاطر این هس که کلماتی رزرو شده در mysql وجود داره و اگه نام جدول ، دیتابیس ، فیلد هم نام اونا باشن mysql نمی تونه تشخیص بده که این اون نام رزرو شده نیس . به همین خاطر ما یه کاراکتر ` اضافه میکنیم .
موفق باشی عزیز

Cyletech
جمعه 26 فروردین 1390, 07:48 صبح
فکر کنم مشکلش از این باشه که باید بعد از mysql_query حتماً به متغیری که اتصال به پایگاه داده صورت گرفته وصل بشه. یعنی مثل زیر:

$connect = mysql_connect( 'localhost', 'root', '' );

mysql_query( "SELECT * FROM send WHERE id='$ir' limit 1", $connect);

mysql_query( "UPDATE send SET `today`='$today', `yesterday`='$yesterday', `total`='$total' , `data`='".date('Y-m-d',$data)."' WHERE id='$ir'", $connect);دلیل اینکار این هست که شما وقتی اتصالی میدی باید اون اتصال بفهمه برای کدوم پایگاه داده هست در غیر اینصورت اصلاً خطا نمیده و کار هم نمی کنه!

mtchabok
جمعه 26 فروردین 1390, 09:14 صبح
نه عزیزم
به صورت پیشفرض در توابع mysql_query از آخرین کانکشن به mysql استفاده میکنه ولی اگه لینک کانکش رو بهش بدیم خوب، خیلی بهتره ولی در کل اسکریپت این دوستمون چون در صفحه اش فقط یه کانکشن ساخته بود مشکلی از جهت ارتباط با دیتابیس نداره .