Jump to content


Photo

**SOLVED** algorithm problem.


  • Please log in to reply
11 replies to this topic

#1 dirt

dirt
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 27 March 2006 - 02:32 PM

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.

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 27 March 2006 - 02:42 PM

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
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 dirt

dirt
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 28 March 2006 - 06:38 AM

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...

#4 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 28 March 2006 - 10:19 AM

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 :)
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 March 2006 - 11:22 AM

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

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

This way only a single integer is returned by the query.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 dirt

dirt
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 28 March 2006 - 03:03 PM

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]

#7 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 28 March 2006 - 03:12 PM

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..
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#8 dirt

dirt
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 28 March 2006 - 03:20 PM

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)


#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 March 2006 - 05:33 PM

Does $username have a value?

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#10 dirt

dirt
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 29 March 2006 - 12:03 AM

i have tried everything...i still dont know wat to do.


<?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>";
}


and the output is :-

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


#11 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 29 March 2006 - 08:37 AM

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..
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#12 dirt

dirt
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 29 March 2006 - 10:17 AM

thanks man for the input. i got it workin...some silly syntax error.
it was my fault. Thanks Again




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users