You are not logged in.

#51 08 May 2007 12:15 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: How does the "Elseif" statement work?

they will all be located in $_POST

Code:

foreach($_POST as $key=>$value) {
    if(preg_match("/^check.*/", $key)) {
        echo $value;
    }
}

Offline

 

#52 09 May 2007 4:27 am

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: How does the "Elseif" statement work?

I did the thingy you wrote, and I did yet another google for it, and found this:

http://phpsense.com/php/php-form-handling.html :

Suppose you want to allow the user to select multiple checkboxes as shown in the image below. The logic we applied to the select list above will also apply here. All the checkboxes in the group will have the same name but will have different values.

http://phpsense.com/images/checkboxes.gif

All the checkboxes should have the same name but append [] to this name. For example, utilities[]. Now, $_POST[‘utilities’] will give us an associative array which will be populated with the values of the checkboxes selected by the user.

Note: Only the name attribute is important here. You can set the id attribute to any value you want. You need not append the [] to the checkboxes ids. Moreover, the ids need not be same.

And then I set it to this:
HTML Page: <input type="checkbox" name="check[]" id="' . $row['id'] . '" value="' . $row['mail'] . '" CHECKED>
PHP Page: $check = $_POST['check'];

But when echoing it, it just says ARRAY...

Last edited by Butcher (09 May 2007 5:13 am)


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#53 09 May 2007 8:14 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: How does the "Elseif" statement work?

$check is going to be an array:

Code:

$check = $_POST['check'];

foreach($check as $key=>$value) {
    echo "$key = $value";
}

Offline

 

#54 09 May 2007 9:51 am

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: How does the "Elseif" statement work?

Hmm, ended up as last time I tried the ever so mysterious foreach function;

Code:

Warning: Invalid argument supplied for foreach() in /home/bambooco/public_html/mail/select_new_mail.php on line 42

Though it did work when ran on a seperate page... I would think that a thing such as a HTML checkbox would be much easier to use.


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#55 09 May 2007 2:13 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: How does the "Elseif" statement work?

at any time you can always call:

Code:

echo "<pre>";
print_r($_POST);
echo "</pre>";

to get the contents of post, and if something is an array, it will show up there.  its sometimes helpful to have that in a debug function that you can call if you need to see what actually got posted.

Offline

 

#56 10 May 2007 10:10 am

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: How does the "Elseif" statement work?

I now got this;

Code:

0 = mail1@mail.com1 = mail2@mail.com2 = mail3@mail.com
Array
(
    [check] => Array
        (
            [0] => mail1@mail.com
            [1] => mail2@mail.com
            [2] => mail3@mail.com
        )

    [Submit] =>  Next Page 
))

Which tells me that it works, but only after I added a few changes; I added [] at the end of the name, so it is "check[]" instead of just "check, and I added id="' . $row['id'] . '". Seems to work finally, just need to compile in the last changes and it is ready for use big_smile Thanks for the help once again, never tought a checkbox would bring me any issues to be honest.

So, the word "array" is still the values? So if I do a "mail($check;,subject,message,headers,parameters)", would it then take the values from check and post them into the "to:" field? So that it can send the mails.

Last edited by Butcher (10 May 2007 10:18 am)


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#57 10 May 2007 10:42 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: How does the "Elseif" statement work?

lol, yea you need the [] in the html for it to work.  thats what was blowing the foreach loop up.

when you have an array, and you print it, its going to say array (if it is one), since what is the value of an array? its the individual values of each element in the array, and you can only find that out by iterating over each element and getting its value.

if you're using check as mail address values, then you'll need to a do a foreach or for loop over the elements of the check array.

Offline

 

#58 10 May 2007 1:47 pm

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: How does the "Elseif" statement work?

Ok, well, I tried it in this foreach, with this code:

Code:

$check = $_POST['check'];
foreach($check as $key=>$value) {
    //echo "$key = $value";
}
echo $key = $value;

But the problem is that it only echoes this: To:mail1@mail.com, though all the three existing rows where selected.


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#59 10 May 2007 4:08 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: How does the "Elseif" statement work?

uncomment the echo inside the loop, and remove the bottom one.

its getting all of them, but you're only seeing the last value in the list (the foreach will assign key and value on each iteration.  if you dont echo each one, key and value will retain the last value from the loop).

Offline

 

#60 11 May 2007 6:11 am

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: How does the "Elseif" statement work?

Ahh, ok, yes. I now got this:
0 = To:lol@lolskipolski.com1 = To:omgzomg2 = To:mail@mailbiatch.com

But why does he add the 0 =, 1 =, 2 = etc before the results? I need it to be purely To:<value>, To:<value>, etc, so it works with the mail() system. Is there a way to stop him from numbering them?


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#61 11 May 2007 6:51 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: How does the "Elseif" statement work?

because you're saying echo "$key = $value" so it echo's its index (0, 1, 2...) and its value (TO:lol@lolskipolski.com1 ....)

Offline

 

#62 11 May 2007 8:54 am

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: How does the "Elseif" statement work?

Aha, tought $key was a required part for it to echo properly. Thanks.


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#63 11 May 2007 9:27 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: How does the "Elseif" statement work?

foreach is used to walk the array.  when you use the syntax:

foreach($array as $a=>$b)

it will fill $a with whatever is used to index the array.  if our array was associative (meaning it used a string to index $array["fred"]) then $a would be that associative string.  if our array was simply numeric, then $a would be 0, 1, 2, and so on.

the => operator is used to denote that whatever is on the left of the => is used to store the indexer of the array, and whatever is on the right of => is the value of the array element that its currently at.

you can also say:

foreach($array as $c)

and $c would be filled with the value (similar to just having $b with no $a in the previous way)

Offline

 

#64 11 May 2007 10:06 am

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: How does the "Elseif" statement work?

Ok, I am on the last page of the script now, where foreach gives me a little problem.

Code:

Warning: Invalid argument supplied for foreach() in /home/bambooco/public_html/mail/process_send_mail.php on line 19
A mail with this subject: Subject Test, has been sent:
Message: Message Test
Author: Name Test
Date: 11th of May, 2007
the information was also added to the database, you will be redirected in 10 seconds.

I am using this code for the mail function:

Code:

$check = $_POST['check'];
mail(foreach($check as $to),$subject,$message,$headers);

I also tried a simple:

Code:

$check = $_POST['check'];
foreach($check as $to) {
    echo $to;
}
//mail(foreach($check as $to),$subject,$message,$headers);

To see if it worked, but I get the error. I also changed the previous page, which is the input page (after the page where you select who you mail to), to just $_POST the check info, so that the next page can retrieve it. Can I use it like this:

$check = $_POST['check'];
foreach($check as $to) {}
mail($to,$subject,$message,$headers);

So that it takes all the mails from the check values and insert them into that $to?


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#65 11 May 2007 11:28 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: How does the "Elseif" statement work?

lol, nope those wont work, you'll need something like:

Code:

// make sure you have email to send
if(is_array($check) && count($check) > 0) {
    // iterate over the email addresses, and invoke mail for each address
    foreach($check as $address) {
        // invoke mail for the current address, since we dont care if it fails 
        // we use the @ to suppress any error messages
        @mail($address, $subject, $message, $headers);
    }
}else {
    exit("no emails to mail");
}

Offline

 

#66 11 May 2007 12:24 pm

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: How does the "Elseif" statement work?

Hmm, I need a better way of transfering the $_POST through the pages, right now I have this:

1. Selection Page, select the values you want (mails).

2. Input Page, a simple $check = $_POST['check'];.

3. Process Page, checks if the mails are there.

Problem is, that it haven't got the values when it gets to the process page, I've used a $check = $_POST['check']; there too. Do I need to decode it from its array somehow?


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#67 11 May 2007 1:57 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: How does the "Elseif" statement work?

dont have seperate pages.  what you need to do is place a hidden input in your form.  place presentation (selection and input) and processing on the same page (keep logical contexts within the same page). so that your page that lists things, the page that edits things and the page that processes things is the same page.

if you have a page that displays a list, or processes selected items from that list, you create a form with a hidden value that is statically set.  at the top of the page you either execute one block of code, or another based on if $_POST['hidden_form_input'] was present or not.  if its not present than it means the page is meant to be displaying the list.  if it is present, it means that the form has posted info to the page, and you need to process that data.  after you process the data then you can either redirect to another page, or display the default list.


Code:

<?php

// form processing code... just a simple example
if($_POST['op'] === "process") {
    // process the post back
    @mail($to, $from, $message, $headers);

    // print a thank you message after we process the form post
?>
    <html><head><title>Thank You</title></head><body>Thank You, your comments are appreciated</body></html>
<?php
} else {
?>

<html><head><title>Contact Us</title></head>
<body>
<h1>Contact Us</h1>
<form action="" method="post">
  To: <input type="text" name="to" /><br />
  From: <input type="text" name="from" /><br />
  Message: <textarea name="message"></textarea><br />
  <input type="hidden" name="op" value="process" />
  <input type="submit" />
</form>
</body>
</html>

<?php
}
?>

Offline

 

#68 11 May 2007 5:47 pm

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: How does the "Elseif" statement work?

I need the three pages because the Selection Page alone will have a lot of rows with nicks and mails on them, it will contain every clanmember in my clan. Tested it a bit, and did this:

1. Select
2. Set in this:

Code:

<input type="hidden" name="to" value="<?php 
          $check = $_POST['check']; 
          foreach($check as $to) 
              {
            echo $to;
            }          
          ?>">

3. Set this:

Code:

$to = $_POST['to'];
@mail($to, $subject, $message, $headers);

And it worked, I got the message big_smile


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#69 25 Nov 2007 4:03 am

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: How does the "Elseif" statement work?

Figured this question fit best in this post;

I very recently aquired a MySQL copy of the CDDB (FreeDB CD Database), that contains 9 million CDs and 31 million CD Tracks, but they have arranged it in a special manner:

The database, CDDB, contains 3 tables, categories, discs and tracks, both discs and tracks have a column named discid, used to bind the tracks to the disc, but also used to identify them to each other, is category (genre best fitting for the disc). But here is the tricky part, there are several duplicates of each CD based on what genre it fits in, so the discid 2282808330 would not only show "Iron Maiden - A matter of Life and Death", but 4 CDs, all with same artist and title, but different categories.

So, if I queried the database, retrieving artist, title and discid, comparing that to tracks to show tracks for the CD, I would not get the 10 songs on the CD, I would get all 40 songs, for all the 4 CDs. So the question is, how can I force the SQL query to only output tracks from one category, and make sure that category actually exists with the CD?

I figured I could compare the CDDB categories (genres) to the ones in my own CD database, and show tracks based on the category ID in my database, but the problem is that some categories I have don't exist in the CDDB, and in the interest of actually showing the tracks, I need to find a way to have it check how many categories exist for the given discid, then make sure it is the same artist and title, and then limit it to a single category (whichever category that would output, as I don't use their genre system, but my own) and finally putting that in the SQL query to only allow it to show tracks based on that category.

Any suggestions on how to accomplish this?

I know this seems like a crazy thing to do, but it is far easier than submitting the tracks myself, as I am putting somewhat close to 3000 CDs into the CD database (my fathers CD collection, to be archived digitally), and that will go a whole lot faster if I don't have to write in each track as well as the Artist and Title.

Last edited by Butcher (25 Nov 2007 4:04 am)


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#70 26 Nov 2007 11:16 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: How does the "Elseif" statement work?

I don't understand how or why category is even considered at this point, but...

I'm assuming the following schema:

discs:
- discid
- artist_name
- album_name
- <other columns not being used here>

tracks:
- id
- discid
- track_name
- <other columns not being used here>


SELECT discs.artist_name, discs.album_name, tracks.track_name FROM discs LEFT JOIN tracks ON discs.discid = tracks.discid WHERE discs.discid=SOME_VALUE;

the above statement will join up the discs table and tracks table (on the discid column) and will return artist name, album name, and track name in one row where the disc id is some value that you pass in.

Offline

 

#71 26 Nov 2007 1:49 pm

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: How does the "Elseif" statement work?

It actually is:

category:
- category (number)                                  
- name (genre name)

discs:
- category (number, matches category table)
- discid (number, matches tracks table)
- genre (ridiculous considering there is the category table)
- artist (should match tracks artist, but doesn't)
- title (obvious)
- year (obvious)

tracks:
- category (number, matches category table)
- discid (number, matches discs table)
- artist (mostly assigned as "NULL")
- title (track name)

So, to retrieve a single CD, you would have to match the discid and the category, and both must be indentical, if category is not specified, you will get the same CD again and again, based on it most likely being submitted in different categories (hence the size of the CDDB database, 1.6 GB). So I need to check all available numbers from a CD (from category), then take any one of them and put that into the WHERE clause, together with discid, artist and title.


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#72 26 Nov 2007 8:35 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: How does the "Elseif" statement work?

ok, let me get this straight... there are multiple records per discid, and each record is a duplicate, only category number is different???

thats terrible database form.  there really needs to be a seperate table joining discs and category.  I would port that database over to something proper.

here's a query that will select a single set of records.

Code:

SELECT
    discs.discid,
    discs.artist,
    discs.title,
    tracks.title
FROM
  (
    SELECT
     *
    FROM
      discs
    WHERE
        discs.discid = SOME_VALUE
    LIMIT 1,1
  ) AS discs
LEFT JOIN
  tracks
ON
  discs.discid = tracks.discid
WHERE
    discs.discid = SOME_VALUE
ORDER BY
  tracks.title ASC

again, you'll need to replace both SOME_VALUE's with whatever discid you need.

this query starts out by filtering out all but 1 record from the discs table.  normally when you do "FROM discs ..." it uses every record in the discs table as your starting point.  adding the sub-select query "(SELECT * FROM discs WHERE discs.discid = SOME_VALUE LIMIT 1,1) AS discs" creates a temp table (which i name discs just to make it easy to read) from which to start the rest of the query.  since the starting table will contain either 1 or 0 rows, the LEFT JOIN clause will use that 1 discs record to join in its track information (and / or category info if you wanted that in there too).

again, unless you want the category info, its not really needed here.

Offline

 

#73 27 Nov 2007 1:26 pm

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: How does the "Elseif" statement work?

I completely agree, it is a very nasty way of using a database. Especially since it creates an overload of rows that should not need to be there.

Code now looks like:

Code:

$dbh=mysql_connect("localhost","user","pass") or die ('MySQL connection invalid: ' . mysql_error() . '<br>');
mysql_select_db("cddb") or die ('MySQL invalid database name: ' . mysql_error() . '<br>');

$where_title = mysql_real_escape_string($_GET['title']); 
$where_artist = mysql_real_escape_string($_GET['artist']); 

// Show Artist and Title
$sq_1 = ("SELECT * FROM discs WHERE artist = '$where_artist' AND title = '$where_title' LIMIT 1");
$result = mysql_query($sq_1) or die ('MySQL error: <b>' . mysql_error() . '</b><br>');
while($row = mysql_fetch_array($result))
{
echo 'Artist: ';
echo $row['artist'];
echo '<br>';
echo 'Title: ';
echo $row['title'];
$disc_id = $row['discid'];
}
$disc_id = $disc_id;
echo '<br><br>' . $disc_id . '';
echo '<br>';
echo '<br>';

// Show tracks
$sq_2 = "SELECT tracks.track, discs.discid, discs.artist, discs.title, tracks.title
FROM (SELECT * FROM `discs` WHERE discs.discid = " . $disc_id . " LIMIT 1) 
AS `discs` LEFT JOIN tracks ON discs.discid = tracks.discid WHERE discs.discid = " . $disc_id . " ORDER BY tracks.track ASC";
$result = mysql_query($sq_2) or die ('MySQL error: <b>' . mysql_error() . '</b><br>');
while($row = mysql_fetch_array($result))
{
echo $row['track'];
echo ' - ';
echo $row['title'];
echo '<br>';
}

And it works just liked I wanted it to big_smile, thanks. Now begins the work of getting all the CD's into the CD database.


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#74 27 Nov 2007 1:42 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: How does the "Elseif" statement work?

yea its not too often you need to do sub-selects.  most databases are formed correctly so you usually just join things up instead of carving up some temp table and querying against it.  its a powerful feature though.  one that I wish was in microsoft's sqlserver was mysql's LIMIT command.  it makes paging results so much easier.

Offline

 

#75 28 Nov 2007 5:44 am

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: How does the "Elseif" statement work?

Ideally I would convert the database to remove all genre and category columns, and remove all discs and tracks except for the most common one by discid, so each discid would be unique and have the correct artist, title and tracks, and there would be no useless duplicates (and the database would be significatly much smaller).

MSSQL doesn't have a limit function? I thought all databases had them. Found one way of faking a limit for MSSQL at this site.


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 



© 2003 - 2019 NullFX
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License