Jump to content


Photo

Making A Query Loop...


  • Please log in to reply
2 replies to this topic

#1 northerncomfort

northerncomfort
  • Members
  • Pip
  • Newbie
  • 5 posts

Posted 12 June 2006 - 02:32 PM

I've encountered a bit of a problem with my contacts database. I'm building a contacts database for my office, which is working pretty nicely so far. The actual contacts part is working perfectly, and is based in mySQL. However, I've also added a "My Contacts" as this was a big request in the office. It uses a simple login, takes the name, creates a table in the database called $username_contacts, etc. Adding a contact to the mycontacts table has been working nicely so far, but I'm having a problem with listing the "My Contacts" part that hopefully I can find some help here with.

Basically, each contact in the main contacts table has an id number. The $username_contacts table keeps track of who you have in your contacts list by the id number.

Here is the code in question. It first checks to make sure you're logged in, then puts your username as $name and grabs the correct table. Then it sees how many contacts you have in my contacts (this is where the script starts to flounder), and for each contact you have in my contacts, it grabs the proper contact info from the main contacts table. However, that isn't working so nicely at the moment, and hopefully somebody here with a beady eye will be able to spot the problem.

Also, is there any way to keep each contact email address in a different variable so I could have an option to display a table of the email addresses so that the folks in the office could quickly email all of "my contacts" at once?

<?php
if($logged_in){
echo "My Contacts";
include "database.php";
$name = $_SESSION['username'];
mysql_connect($connect,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$sql2 = "SELECT * FROM ".$name."_contacts";
$result2 = mysql_query($sql2);
$num=mysql_numrows($result2);
$i = 1;
while ($i <= $num){
$toget = mysql_result($result2,$i,"contact_id");
$sql = "select * from contacts where id='".$toget."'";
$result = mysql_query($sql);
$first=mysql_result($result,$i,"firstname");
$last=mysql_result($result,$i,"lastname");
$company=mysql_result($result,$i,"company");
$address=mysql_result($result,$i,"address");
$city=mysql_result($result,$i,"city");
$state=mysql_result($result,$i,"state");
$zip=mysql_result($result,$i,"zip");
$fax=mysql_result($result,$i,"fax");
$workphone1=mysql_result($result,$i,"workphone1");
$workphone2=mysql_result($result,$i,"workphone2");
$homephone=mysql_result($result,$i,"homephone");
$cellphone=mysql_result($result,$i,"cellphone");
$email=mysql_result($result,$i,"email");
$id=mysql_result($result,$i,"id");
echo "All the usual contact table stuff goes here, it works fine but is bulky so I editted it out for your sake...";
$i++;
}
}
else{
   echo "You are not logged in. You need to be logged in to use My Contacts.<br /><a href=\"index.php\">Log In</a>";
}
?>


#2 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 12 June 2006 - 03:13 PM

first off, you wanna change that long list of mysql_results to this

while ($data = mysql_fetch_array ($result))
{
$field = $data['field']; // and all other stuff which was in the for() loop
}

actually...should look like this:

<?php
if ($logged_in)
{
echo "My Contacts";

include "database.php";

$name = $_SESSION['username'];

mysql_connect ($connect, $username, $password);
mysql_select_db ($database) or die( "Unable to select database");

$sql = "SELECT * FROM ".$name."_contacts";
$result = mysql_query ($sql) or die (mysql_error());
while ($data = mysql_fetch_array ($result))
{
    $first = $data['firstname']; //etc
}
}
else
{
   echo "You are not logged in. You need to be logged in to use My Contacts.<br /><a href=\"index.php\">Log In</a>";
}
?>

z..z..z..z..z..z..z..z..

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 12 June 2006 - 03:26 PM

try
$sql2 = "SELECT * FROM ".$name."_contacts";
$result2 = mysql_query($sql2);

$all_contacts = array();
while ($row = mysql_fetch_assoc($result2)) { 
    extract($row);
    $all_contacts[] = $email;
}
   // put all email addresses into comma-delimited string
$to_all = join (',' , $all_contacts);

Why a separate table per user? Add username column then

SELECT * FROM contacts WHERE username = '$name';
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users