Jump to content

[SOLVED] help with array and comparisons


40esp

Recommended Posts

Alright, here it goes lol.

 

I have a database table that keeps track of email newsletter sign ups. each record looks like:

 

TABLE: newsletter_signups

 

ID         email             newsletters_signed_up         Active

1     [email protected]           [2] [3] [4]                         Y

 

 

The numbers in newsletters_signed_up represent specific newsletter ID's displayed like:

 

TABLE: newsletters

 

newsletter_id           title

    1                      Title1

    2                      Title2

    3                      Title3

    4                      Title4

 

in a seperate table.

 

What I need to do is create an array from "newsletters" with the specific ID's, I have the connection as so:

 

<?php mysql_select_db($database_sw, $sw);
$query_practice_groups = "SELECT * FROM newsletters ORDER BY title ASC";
$practice_groups = mysql_query($query_practice_groups, $sw) or die(mysql_error());
$row_practice_groups = mysql_fetch_assoc($practice_groups);
$totalRows_practice_groups = mysql_num_rows($practice_groups); ?>

 

How would I make an array using data given from this connection?

 

my next question is, how do I compare all of the values from that array, with a variable lets say its called:

 

$is_subscribed = $row_email_record['newsletters_signed_up'];

 

Where the value is being retrieved from a POST variable, and checked against the table newsletter_signups, then returning the specific record based on the "email" field.

 

The connection is as so:

<?php
mysql_select_db($database_sw, $sw);
$query_email_record = sprintf("SELECT * FROM newsletter_signups WHERE email = %s", GetSQLValueString($_POST['email'], "text"));
$email_record = mysql_query($query_email_record, $sw) or die(mysql_error());
$row_email_record = mysql_fetch_assoc($email_record);
$totalRows_email_record = mysql_num_rows($email_record); ?>

 

if the newsletters_signed_up result is [2] [3] [4]  I want to check the array built and if any of these numbers match the [2] and [4] in newsletters_signed_up I want it to do something like this:

 

[1]

[2] SUBSCRIBED

[3] SUBSCRIBED

[4] SUBSCRIBED

 

 

I hope I've explained this well enough.

Thanks to everyone that takes a stab at this   :)

 

 

Link to comment
https://forums.phpfreaks.com/topic/109468-solved-help-with-array-and-comparisons/
Share on other sites

 

You can read newsletters into an array like this:

<?php 
mysql_select_db($database_sw, $sw);
$query_practice_groups = "SELECT * FROM newsletters ORDER BY title ASC";
$practice_groups = mysql_query($query_practice_groups, $sw) or die(mysql_error());
$totalRows_practice_groups = mysql_num_rows($practice_groups);
while ($row_practice_groups = mysql_fetch_assoc($practice_groups)) 
{
  $newsletter[$row_practice_groups['newsletter_id']] = $row_practice_groups['title'];
}
?>

 

So, $newsletter will be an array of the titles with the keys are their id numbers.

 

Then, you can check the newsletters_signed_up field against those keys like this:

<?php
foreach($newsletter as $key=>$title)
{
  if (strstr($is_subscribed, "[$key]")) echo "[$key] SUBSCRIBED\n"; else echo "[$key]\n";
}
?>

 

Hope this is what you're looking for.

Archived

This topic is now archived and is closed to further replies.

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