homepage Welcome to WebmasterWorld Guest from 54.205.106.111
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Slight problem
gruntre




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

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

 

dreamcatcher




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

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

gruntre




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

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

henry0




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

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

grandpa




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

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.

henry0




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

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?

gruntre




msg:1245503
 2:36 am on Mar 12, 2005 (gmt 0)

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

gruntre




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

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

gruntre




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved