?
Solved

WHILE LOOP PHP BEING A PAIN! Help please

Posted on 2011-03-17
11
Medium Priority
?
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
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 2000 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

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…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

741 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