Jump to content

Archived

This topic is now archived and is closed to further replies.

dirt

**SOLVED** algorithm problem.

Recommended Posts

i have this online dvd rental system... i need to make sure the priority is set for every
single members level...i have set their priority but i need a output that says "You have rented X amount of DvD's" once the member hits the submit button.

Share this post


Link to post
Share on other sites
you would have to have this on another page like on the page that the form submits too but you can just use a query simular to..


[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$query=myslq_query("SELECT * FROM table_name WHERE rented_by = '$username'");
$rented=mysql_num_rows($query);
echo("You have rented $rented DVD's");[/quote]


You will need to change table_name to the tablename and also rented_by to the field in your table which store's the username that is currently renting that DVD

Share this post


Link to post
Share on other sites
but what if i did the codings differently...somethimg like this

if(substr($key,0,3)=="buy"){
query="INSERT INTO buy ( user , movies )VALUES ('".$_COOKIE['usrname']."', '".$val."');"; mysql_query($query, $ren) or die(mysql_error());

where do i put echo("You have rented $ DVD's");??

i dont mind showin the echo output in the same page...

Share this post


Link to post
Share on other sites
if(substr($key,0,3)=="buy"){
$query="INSERT INTO buy ( user , movies )VALUES ('".$_COOKIE['usrname']."', '".$val."');"; mysql_query($query, $ren) or die(mysql_error());
$query=myslq_query("SELECT * FROM buy WHERE user = '$username'");
$rented=mysql_num_rows($query);
echo("You have rented $rented DVD's");
}

That should work mate :)

Share this post


Link to post
Share on other sites
The time to execute a query is proportional to the amount of data returned, so selecting data then counting rows is a very inefficient way of getting a count. Using "SELECT * " makes it doubly inefficient.

Use

[code]$query=myslq_query("SELECT COUNT(*) FROM buy WHERE user = '$username'");
$rented=mysql_result($query, 0);
echo("You have rented $rented DVD's");[/code]

This way only a single integer is returned by the query.

Share this post


Link to post
Share on other sites
i am not getting the output that i want.is there anything i need to add...or mayb i forgot to do something

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]<?php require_once('Connections/ren.php'); ?><?php
mysql_select_db($database_ren, $ren);
$query= "SELECT membership FROM user WHERE username='".$_COOKIE['usrname']."'";
$usr = mysql_query($query, $ren) or die(mysql_error());
$row_usr = mysql_fetch_assoc($usr);
$limit=$row_usr['membership'];

$count=0;
if (isset($_POST['save']) && (isset($_COOKIE['usrname'])) ) {
foreach($_POST as $key => $val){
if(substr($key,0,3)=="buy"){
$query="INSERT INTO buy ( user , movies )VALUES ('".$_COOKIE['usrname']."', '".$val."');";
mysql_query($query, $ren) or die(mysql_error());
$query=myslq_query("SELECT COUNT(*) FROM buy WHERE user = '$username'");
$rented=mysql_result($query, 0);
echo("<center><font color=red>You have rented $rented DVD's </font></center>");[/quote]

Share this post


Link to post
Share on other sites
So what is it outputting? and what are you wanting it to output like as we have oviusly got the wrong idea about what you are wanting..

Share this post


Link to post
Share on other sites
i need to get the outcome that says a customer has bought X ammount of DVD either in the same page or diff page.

I am reloading to the same page when i click the submit button(but without the outcome)

Share this post


Link to post
Share on other sites
Does $username have a value?

If not, before the query
[code]$username = $_COOKIE['usrname'];[/code]

Share this post


Link to post
Share on other sites
i have tried everything...i still dont know wat to do.


[code]<?php require_once('Connections/ren.php'); ?><?php
mysql_select_db($database_ren, $ren);
$query= "SELECT membership FROM user WHERE username='".$_COOKIE['username']."'";
$usr = mysql_query($query, $ren) or die(mysql_error());
$row_usr = mysql_fetch_assoc($usr);
$limit=$row_usr['membership'];

$count=0;
if (isset($_POST['save']) && (isset($_COOKIE['usrname'])) ) {
   foreach($_POST as $key => $val){
         if(substr($key,0,3)=="buy"){
$query="INSERT INTO buy ( user , movies )VALUES ('".$_COOKIE['usrname']."', '".$val."');";
mysql_query($query) or die(mysql_error());
$username = $_COOKIE['usrname'];
$query=mysql_query("SELECT COUNT(*) FROM buy WHERE user = '$username'");
$purchsed=mysql_num_rows($query);
mysql_close();
echo("<center><font color=red>You have purchased $purchased DVD(s) </font></center>");}
    
      }
if($count < $limit){
      if(substr($key,0,4)=="rent"){
$query="INSERT INTO rent ( user , movie )VALUES ('".$_COOKIE['username']."', '".$val."');";      
    mysql_query($query, $ren) or die(mysql_error());
$username = $_COOKIE['usrname'];
$query=mysql_query("SELECT COUNT(*) FROM buy WHERE user = '$username'");
$rented=mysql_num_rows($query);
mysql_close();
echo("<center><font color=red>You have purchased $rented DVD(s) </font></center>");
              }
      }else{
        echo "<center><font color=red>Limit Exceeded!! Only $limit is rented!! </font></center>";
      }
   }
}
if (isset($_POST['save']) && (!isset($_COOKIE['username'])) ) {
   echo "<center><font color=red>Please Login</font></center>";
}

[/code]

and the output is :-

You have purchased DVD's
Limit Exceeded!! Only is rented!!
Limit Exceeded!! Only is rented!!
Limit Exceeded!! Only is rented!!

Share this post


Link to post
Share on other sites
you have used part my code and part barand's

so your whole code should be

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]<?php require_once('Connections/ren.php'); ?><?php
mysql_select_db($database_ren, $ren);
$query= "SELECT membership FROM user WHERE username='".$_COOKIE['username']."'";
$usr = mysql_query($query, $ren) or die(mysql_error());
$row_usr = mysql_fetch_assoc($usr);
$limit=$row_usr['membership'];

if (isset($_POST['save']) && (isset($_COOKIE['usrname'])) ) {
foreach($_POST as $key => $val){
if(substr($key,0,3)=="buy"){
$query="INSERT INTO buy ( user , movies )VALUES ('".$_COOKIE['usrname']."', '".$val."');";
mysql_query($query) or die(mysql_error());
$username = $_COOKIE['usrname'];
$query=myslq_query("SELECT COUNT(*) FROM buy WHERE user = '$username'");
$count=mysql_result($query, 0);
mysql_close();
echo("<center><font color=red>You have purchased $purchased DVD(s) </font></center>");}

}
if($count < $limit){
if(substr($key,0,4)=="rent"){
$query="INSERT INTO rent ( user , movie )VALUES ('".$_COOKIE['username']."', '".$val."');";
mysql_query($query, $ren) or die(mysql_error());
$username = $_COOKIE['usrname'];
$query=myslq_query("SELECT COUNT(*) FROM buy WHERE user = '$username'");
$rented=mysql_result($query, 0);
mysql_close();
echo("<center><font color=red>You have purchased $rented DVD(s) </font></center>");
}
}else{
echo "<center><font color=red>Limit Exceeded!! Only $limit is rented!! </font></center>";
}
}
}
if (isset($_POST['save']) && (!isset($_COOKIE['username'])) ) {
echo "<center><font color=red>Please Login</font></center>";
}[/quote]

and see how that works :)

i also made your $count an actual count of what they have already purchased as you had set it to 0 which is always less than your limit..

Share this post


Link to post
Share on other sites
thanks man for the input. i got it workin...some silly syntax error.
it was my fault. Thanks Again

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.