Jump to content

select multiple form and querying mysql table


blingcr

Recommended Posts

Hi.  I like to gamble on sports (and no, i'm not hardcore or anything, very small bets ranging from $1-4 usually, its just for fun), and not that long ago, decided it would be more efficient to store my betting log in a database, rather than having to update a bunch of things using a spreadsheet.  The problem was I didn't know anything about MySQL or PHP.  Well, for the past month or so I've been reading as much as I can, and studying up on things whenever I get the spare time.  I managed to create a table, and a form shortly after where I can insert my bet into the table.  That all seemed to be fairly easy...now on to the main reason i wanted to move to a db...

 

I wanted to be able to search the table with a form...not so much with text searches, that seems like it would be easy to implement later on...but...say (for example) how I did in 2007...or more specifically, in December of 2007, or more specifically, on Sundays in December of 2007, etc....basically, this is what it looks like:

form.jpg

So it's nothing really special, just something for me to play around with.  All of them are multiple select so that passes $_POST['whatever'], which, I guess, is all ready an array, as an array in an array.  Basically, here's a row out of my table to give you an idea of how it looks:

mysql.jpg

The actual select names in the form are date(which is misleading because I mean year), month, day, sport, and stars.  Now I'll try to post some code.  I took all the selections and, as far as I'm concerned, renamed them to a less confusing array name.  I'm sure that wasn't necessary...but it just seemed a little easier for me to look at.

// counting
$date_count = count($_POST['date']);
$month_count = count($_POST['month']);
$day_count = count($_POST['day']);
$sport_count = count($_POST['sport']);
$stars_count = count($_POST['stars']);

foreach ($_POST['date'] as $year_push) {
array_push($year, $year_push);
}

foreach ($_POST['month'] as $month_push) {
array_push($month, $month_push);
}

foreach ($_POST['day'] as $day_push) {
array_push($day, $day_push);
}

foreach ($_POST['sport'] as $sport_push) {
array_push($sport, $sport_push);
}

foreach ($_POST['stars'] as $stars_push) {
array_push($stars, $stars_push);
}

Now, at this point, I simply want to print out a record of bets won vs bets lost for the specific period I've chosen within the form before I try anything else.  But I just can't seem to get it right.  Printing out a record as a WHOLE seems to be no problem though.

$wins = mysql_query("SELECT COUNT(w_l) FROM wagers
WHERE w_l='Win'");
$losses = mysql_query("SELECT COUNT(w_l) FROM wagers
WHERE w_l='Loss'");
$pushes = mysql_query("SELECT COUNT(w_l) FROM wagers
WHERE w_l='Push'");

// Printing out the actual record
echo "<b>Record: </b>";
while ($row = mysql_fetch_array($wins)) {
echo $row["COUNT(w_l)"];
$wpct = $row["COUNT(w_l)"]; // stores the # to calc winning %
}

echo " - ";
while ($row = mysql_fetch_array($losses)) {
echo $row["COUNT(w_l)"];
$lpct = $row["COUNT(w_l)"]; // stores the # to calc winning %
}

echo " - ";
while ($row = mysql_fetch_array($pushes)) {
echo $row["COUNT(w_l)"];
}

// This little confusing line just calculates a winning percentage
echo " - ". round(($wpct / ($wpct + $lpct)) * 100, 1) ."%";

 

Then came the delima...I have no idea how to plug in all the selections from the form...well, I have ideas, they just aren't efficient...I don't think a million lines of elseif statements with a count of number of items in each array and a mysql query will do me much good.  I can sit there in the MySQL command line and query the way I'd like.

 

I started doing something like this...but, as you can probably tell, by the end of it, I'll be left with some rather long queries...and that leaves me thinking something will come out terribly wrong since it would be adding in elements of arrays that don't even exist 99% of the time.  But here is what I started on that I feel just won't work...or there just has to be a better way...

// MySQL Queries
$wins = mysql_query("SELECT COUNT(w_l) FROM wagers
WHERE (date LIKE '$year[0]%' OR date LIKE '$year[1]%')
AND w_l='Win'");
$losses = mysql_query("SELECT COUNT(w_l) FROM wagers
WHERE (date LIKE '$year[0]%' OR date LIKE '$year[1]%')
AND w_l='Loss'");
$pushes = mysql_query("SELECT COUNT(w_l) FROM wagers
WHERE (date LIKE '$year[0]%' OR date LIKE '$year[1]%')
AND w_l='Push'");

...and that's just for the year!  So I don't know what to do.

 

I found some sample code from one of the tutorials at this site:

http://www.phpfreaks.com/quickcode/Get-data-from-multiple-fields-&-tables/666.php

 

I'm not sure if this is what I want...I don't really understand it that well.  As hard as I've tried studying this stuff...so much of it is still just way over my head.  I hope I've made sense in what I'm trying to do and provided enough information in this post for someone to point me in the right direction.  Thanks for your time

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.