Solved

WHILE LOOP PHP BEING A PAIN! Help please

Posted on 2011-03-17
11
241 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 142

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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 27

Expert Comment

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

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 142

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now