Welcome to WebmasterWorld Guest from 54.145.144.101

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Slight problem

   
5:05 am on Mar 11, 2005 (gmt 0)

10+ Year Member



Can anybody help me with a simple mod to this code.
I borrowed it from a tutorial and modified it for my use but I would like to change it to only display customer email adresses which have "YES" in the extra field.

<?php
include("config.php");
// Connect to the database server
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database $DBName");

mysql_select_db($DBName) or die("Unable to select database $DBName");
?>
<p> Here are all the email address in our orders database:
</p>
<blockquote>
<?php
$result = @mysql_query("SELECT email FROM
orders");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() .
"</p>");
exit();
}
while ( $row = mysql_fetch_array($result) ) {
echo($row["email"] . "<br>");
}
?>

9:02 am on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Hi Gruntre,

Add an if statement inside your while loop:


while ( $row = mysql_fetch_array($result) ) {
if ($row['fieldname']=="YES")
{
echo($row['email'] . "<br>");
}
}

This would display only e-mail addresses that YES is specified in whatever your extra field is called. Hope that helps.

dc

11:23 am on Mar 11, 2005 (gmt 0)

10+ Year Member



Thanks for your help DC,
It doesn't work, it went from displaying all 3 addresses to none of then. 1 of them has Yes please subscribe me and the other 2 have NULL.

<?php
include("config.php");
// Connect to the database server
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database $DBName");

mysql_select_db($DBName) or die("Unable to select database $DBName");
?>
<p> Here are all the email address in our orders database:
</p>
<blockquote>
<?php
$result = @mysql_query("SELECT email FROM
orders");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() .
"</p>");
exit();
}
// while ( $row = mysql_fetch_array($result) ) {
// echo($row["email"] . "<br>");
// }

while ( $row = mysql_fetch_array($result) ) {
if ($row['edata']=="Yes subscribe me")
{
echo($row['email'] . "<br>");
}
}
?>

I am hoping to use this to make it easier for me to add my customers to my mailing list. I want to eventually have it create a field with a yes value for added or maybe check the list DB itself to see if they are added or not and only display the ones that need to be added.

I am trying hard to learn this code but am having trouble with the syntax and structure at this stage.

Your help is much appreciated and I'm sure it will increase my learning curve.

Cheers

Gruntre

12:26 pm on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Unless I miss something why do not you add a "where" clause in your query for ex: where yes=yes
if you have a col named yes

$sql = "select * from XXXX where yes = '$yes'";
$yes being whatever is entered as Y or yes etc...

12:35 pm on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes please subscribe me and the other 2 have NULL.

while ( $row = mysql_fetch_array($result) ) {
if ($row['edata']=="Yes subscribe me")
{

Are you certain that you are testing the right condition? It appears that your database contains 'Yes please subscribe me' and you are testing for 'Yes subscribe me'. That will return... nothing.

I am trying hard to learn this code but am having trouble with the syntax and structure at this stage
Your troubles will melt away in no time.

:) I'm an old programmer but still green with PHP. I bet I only use about 10 PHP functions, and there appear to be more than 100 available. Maybe someone knows the exact number...

In addition to knowing the language specific requirements, I find it helpful to having a broader understanding of the programming cycle. One thing that helped me early on was a class that discussed 6 Steps of Successful Programming. I can't recall the specifics, but believe me when I say that the process happens before I write a program. That's especially helpful for designing a structure for your code. You could look into psuedo code for more structuring information.

1:15 pm on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Indeed I did not pay attention to the full "Yes please etc..."
However I am back to my initial statement why enter in the DB a whole sentence when "1 or 0", or "y or n" will be sufficient?
you can offer the whole sentence prior to click "Yes ..." but the value would be Y or N
as I mentioned if y or n are entered why not looking to select where y or n are there?
2:36 am on Mar 12, 2005 (gmt 0)

10+ Year Member



Thanks again for the advise and help. I admitt I am a little out of my depth here.
The reason Yes subscribe me is the entry is because it is input posted to the db from a 3rd party shopping cart which I only have limited control of.
It does come in via my script so I guess I could chang it to Y or N but this is a minor detail.

Unless I miss something why do not you add a "where" clause in your query for ex: where yes=yes
if you have a col named yes
$sql = "select * from XXXX where yes = '$yes'";
$yes being whatever is entered as Y or yes etc...


The column name is extra and the posible entries are "Yes subscribe me" and "No"
I would like to try this:
$sql = "select * from extra where yes = '$Yes subscribe me'"; 
but am not 100% sure where to put it and how $sql is relevent to the rest of the code.

Gruntre

2:55 am on Mar 12, 2005 (gmt 0)

10+ Year Member



Maybe that should be:
$yes = "Yes subscribe me"; 
$result = @mysql_query("SELECT email FROM
orders WHERE extra = '$yes'");

but that doesn't work either?
It returns nothing!

Gruntre

3:08 am on Mar 12, 2005 (gmt 0)

10+ Year Member



Yahoo! I got it, thanks HEAPS guys fo putting up with my feeble attempts at learning PHP. It was a dumb error with the column name. Here it is working:
include("config.php");
// Connect to the database server
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database $DBName");

mysql_select_db($DBName) or die("Unable to select database $DBName");
?>
<p> Here are all the email address in our orders database:
</p>
<blockquote>
<?php
$yes = "Yes subscribe me";
$result = @mysql_query("SELECT email FROM
orders WHERE edata = '$yes'");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() .
"</p>");
exit();
}
while ( $row = mysql_fetch_array($result) ) {
echo($row["email"] . "<br>");
}
?>


THANKS AGAIN!

Gruntre

 

Featured Threads

Hot Threads This Week

Hot Threads This Month