Solved

WHILE LOOP PHP BEING A PAIN! Help please

Posted on 2011-03-17
11
243 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

910 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

25 Experts available now in Live!

Get 1:1 Help Now