Solved

WHILE LOOP PHP BEING A PAIN! Help please

Posted on 2011-03-17
11
249 Views
Last Modified: 2012-05-11
Hi Experts,

This while loop is createing the output over and over and over in a seperate box. All i need it to do is to add the information into the last spoken to and the notes box. rather than recreate seperate tables. is this possible?? Ive attached my code.
<?php 
error_reporting(E_ALL);
include 'myphp.php';
$val = $_GET['val'];

$sql = "select * from customer, notes where customer.customer_name = notes.customer_name AND customer.customer_name = '" . $val . "'"; 
$result = mysql_query($sql) or die(mysql_error() . " IN $sql"); 
$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
   echo"<table>";
   echo "<tr><td>Company:</td><td>".$row['customer_name']."</td></tr>";
    echo "<tr><td>Account Number:</td><td><a href=\"account.php?val=".$row['account_number']."\">".$row['account_number']."</a></td></tr>";
	 echo "<tr><td>Contact Name:</td><td>".$row['contact_name']."</td></tr>";
	 echo "<tr><td>Telephone Number:</td><td>".$row['telephone_number']."</td></tr>";
	 echo "<tr><td>Fax:</td><td>".$row['fax']."</td></tr>";
	 echo "<tr><td>Contact Mob Number:</td><td>".$row['contact_mobile']."</td></tr>";
	 echo "<tr><td>Address:</td><td>".$row['full_address']."</td></tr>";
	 echo "<tr><td>Network:</td><td>".$row['network']."</td></tr>";	 
	 echo "<tr><td>Last Spoke To:</td><td>
<div style=\"width: 190px;height: 250px;overflow:auto;\">"
.$row['last_spoke_to']."</div></td></tr>";
echo "<tr><td>Notes:</td><td>
<div style=\"width: 190px;height: 250px;overflow:auto;\">"
.$row['notes']."</div></td></tr>";
	 echo "<tr><td>Kit Fund Level:</td><td>".$row['kit_fund_level']."</td></tr>";
   echo"</table>";
}
   ?>

Open in new window

0
Comment
Question by:NeoAshura
  • 6
  • 3
  • 2
11 Comments
 
LVL 6

Author Comment

by:NeoAshura
ID: 35154900
I should of explained a little better, there is only one data field for Customer name, telephone, fax, mob, address and network..

but Last spoke to and notes are from a diffrent table where there is more than one entry.

it creates a new table for every notes entry when all i require is for it to be added to the exsisting table.

is this possible?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35154947
you have to move the  following 2 lines outside the loop:
echo"<table>";  // this has to go before the loop
echo"</table>"; // this has to go after the loop
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35154951
Move the <table> and </table> outside the loop.
Does the query run ok through phpmyadmin ?
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 6

Author Comment

by:NeoAshura
ID: 35155042
thanks for the attempt guys but that didnt solve it either. It repeats the data still, Last spoke to and Notes are from a different table but it repeats all data. have a look at the screenshots i provided but try and imagine them joined together on the same page. thats the problem im having.
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35155058
sorry forgot the screen shots didnt i...

can you see the data above the last spoke to and notes is the same but the last spoke to and notes entry is diffrennt.. it repeats all of the table but i only need it to add the last_spoke to and notes bit to one box and not two
picture.jpg
picture2.jpg
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35155062
There are no screenshots... ?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35155102
I see.

well, either you make this in 2 queries, or you put the account information only on the first "loop"...

what happens if there are 0 notes for a customer?
you need a left join ...

something like this:
<?php 
error_reporting(E_ALL);
include 'myphp.php';
$val = $_GET['val'];

$sql = "select * from customer left join notes on customer.customer_name = notes.customer_name WHERE customer.customer_name = '" . $val . "'"; 
$result = mysql_query($sql) or die(mysql_error() . " IN $sql"); 
$rs = mysql_query($sql);

echo"<table>";
$row_count = 0;

while($row = mysql_fetch_array($rs))
{
   $row_count ++;
   if ($row_count == 1)
   {
     echo "<tr><td>Company:</td><td>".$row['customer_name']."</td></tr>";
     echo "<tr><td>Account Number:</td><td><a href=\"account.php?val=".$row['account_number']."\">".$row['account_number']."</a></td></tr>";
     echo "<tr><td>Contact Name:</td><td>".$row['contact_name']."</td></tr>";
     echo "<tr><td>Telephone Number:</td><td>".$row['telephone_number']."</td></tr>";
     echo "<tr><td>Fax:</td><td>".$row['fax']."</td></tr>";
     echo "<tr><td>Contact Mob Number:</td><td>".$row['contact_mobile']."</td></tr>";
     echo "<tr><td>Address:</td><td>".$row['full_address']."</td></tr>";
     echo "<tr><td>Network:</td><td>".$row['network']."</td></tr>";  
     echo "<tr><td>Last Spoke To:</td><td>
<div style=\"width: 190px;height: 250px;overflow:auto;\">"
.$row['last_spoke_to']."</div></td></tr>";
  }
  echo "<tr><td>Notes:</td><td>
<div style=\"width: 190px;height: 250px;overflow:auto;\">"
.$row['notes']."</div></td></tr>";
         echo "<tr><td>Kit Fund Level:</td><td>".$row['kit_fund_level']."</td></tr>";
}
   echo"</table>";
   ?>

Open in new window

0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35155262
Hi thanks that fixed the repeat of the company data..... however.. how would i go about only getting one notes box and one last_spoke to box, The last spoke to box also only appears once when it should appear multiple times like the notes box is there antoher join needed here?

i appricate all your time i would be lost otherwise.

please see picture attached
picture4.jpg
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35155312
I am open to changeing Database structure and insert query to get around this problem too. I have attached my insert query which may be the cause of the problem.

If there is a way of just adding information to an already exsisiting database entry then that may be an option too.

ive attached my insert code.
<?php
session_start();
ob_start();
?>
<?php 
error_reporting(E_ALL);

$last_spoke_to = $_POST['last_spoke_to'];
$notes = $_POST['notes'];
$val= $_REQUEST['val'];
var_dump($val); 
var_dump($notes); 


include 'myphp.php';


//update the database with the set variables recoverd from the form from the other page. 

$query = "INSERT INTO `notes` (customer_name, notes, last_spoke_to) VALUES ('".$val."','".$notes."','".$last_spoke_to."')";


$result=mysql_query($query);

 if($result){
	header("location:mobilenum.php?val=".$val);
}
else{
	echo "ERROR";
}
mysql_close();

?>

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35155432
UPDATE means: update an existing record.
INSERT means: create a new record.

so, what exactly do you want?
anyhow, this goes beyond the original question, no?
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35155454
indeed ill reward and open new question which hopefully you can assist in.

many thanks
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question