Concatenating Queries and Creating links that use get to send ID

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 2382 Views - Last Post: 10 May 2011 - 05:03 AM Rate Topic: -----

#1 jaimesharp  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 27-May 09

Concatenating Queries and Creating links that use get to send ID

Posted 09 May 2011 - 01:42 AM

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Prac 3 Task 9</title> </head> <body>
<?php
{
    $conn = mysql_connect("localhost", "twastudent", "prac3");
    
    if (!$conn)
          {
          die('Could not connect: '. mysql_error());
          }
    
    mysql_select_db("warehouse", $conn) or die('Database not found '. mysql_error() );
    
    mysql_select_db("warehouse", $conn);

$sql = "SELECT customerID,firstName, lastName FROM customer";
     $rs = mysql_query($sql,$conn);

    if (mysql_num_rows($rs)>0)
        {
    ?>
        <table border="1" summary=" Customer Order Details">
        <tr>
        <th>Customer ID</th>  <th>First Name </th> <th> Last Name </th>
        </tr>
    <?php
        while ($row =  mysql_fetch_assoc($rs))
            {
    ?>
        <tr>
        <td><?php echo $row['customerID']; ?></td>
        <td> <?php echo $row['firstName']; ?></td>
        <td><?php echo $row['lastName']; ?></td>
        </tr>
  
    <?php
             } //end while
    ?>

        </table>
<?php
        }
    else
        {
?>
            <br><br>No customer found with id  <?php echo $_GET['custID']; ?> in order database.</p>
           
<?php
        }
mysql_close($conn);
}
?>



This is the code form my previous example though now i am pulling in Customer ID,First Name and Last Name


Basically I want to Concatinate the first and last name after they are queried from the database and create dynamic links to my previous PHP file so that when a users name is clicked the id is sent using GET to the PHP file

Though i am happy with lastname only as it is easier to code i have it pulling this in though i would need to link the last name as an anchor if this method is chosen to us GET customer ID

I know in javascript etc how to generate the dynamic links using a for loop <.length

Though using the while loop would get the same but how would i set the anchor to the php file to send that ID

i know how to set an anchor link would it need to be a link to the phpfile otherwise <a href = "linkto php file " $GET ["customerID"] though it is going to

'custID'


Any help on this would be much appreciated

This post has been edited by jaimesharp: 09 May 2011 - 01:52 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Concatenating Queries and Creating links that use get to send ID

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Concatenating Queries and Creating links that use get to send ID

Posted 09 May 2011 - 03:45 AM

You can pass variables in the url (GET) like so:
$url = "customers.php?cust_id=".$row['customerID'];

Was This Post Helpful? 0
  • +
  • -

#3 jaimesharp  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 27-May 09

Re: Concatenating Queries and Creating links that use get to send ID

Posted 09 May 2011 - 04:18 AM

okay i get that i think would i call that inside the table as i want it to be when the name is clicked the file is not called customers though your are using that as an example i am assuming bascially the name has to be a hyperlink that passes to the address of.


using your method would i replace the customers.php with the location of the file as this should apply to each member dynamically as an anchored link yours would be

This post has been edited by jaimesharp: 09 May 2011 - 04:20 AM

Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Concatenating Queries and Creating links that use get to send ID

Posted 09 May 2011 - 04:45 AM

I am sorry I guess I didn't understand the problem.
You are filling a table with customers, something like:

ID  FirstName LastName
id1 fname1    lname1
id2 fname2    lname2
.....

you want to be able to click on the FirstName, and then what do you want to happen? where do you want to send the user? to a new page? which page then? or you want to stay on the same page and do what?
Was This Post Helpful? 0
  • +
  • -

#5 jaimesharp  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 27-May 09

Re: Concatenating Queries and Creating links that use get to send ID

Posted 09 May 2011 - 05:03 AM

My Fault yep the table is ID , First name , Last Name though lastname can be the link i would like to Concat

The Names ie Name = first + Last when the name is clicked ie hyperlink it would load an address which is another php file use anything for an example i can change it

this link is meant to use the GET method to send the ID through to a page which i already created to output the associated orders for a customer


ie if last name where id is 3333


the form should get 3333 to create example.php?custID=3333

But i cannot figure out how to make each link name a dynamic link a loop creating an anchor href to the form with a get element $_GET['custID'] for example though this references a textbox but something similar

This post has been edited by jaimesharp: 09 May 2011 - 05:05 AM

Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Concatenating Queries and Creating links that use get to send ID

Posted 09 May 2011 - 05:13 AM

You mean something like:
<?
$str = "";
while ($row =  mysql_fetch_assoc($rs)){
 $str .= "<tr">;
 //print the custome id (first col)
 $str .= "<td>".$row['custID']."</td>";
 /*second co, last name as a link  to example.php with custID passed with GET*/
 $str .= "<td><a href='example.php?custID=".$row['custID']."'>".$row['lastName']."</a></td></tr>";
}
echo $str;
?> 


EDIT: I may have some typos, I am just typing to the browser. but you can get the main idea :)

This post has been edited by japanir: 09 May 2011 - 05:15 AM

Was This Post Helpful? 1
  • +
  • -

#7 jaimesharp  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 27-May 09

Re: Concatenating Queries and Creating links that use get to send ID

Posted 09 May 2011 - 05:20 AM

That seems close as it will generate the links automatically for each user based on their ID i will try it and see Thanks in advance and for your guidance and assistance
Was This Post Helpful? 0
  • +
  • -

#8 jaimesharp  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 27-May 09

Re: Concatenating Queries and Creating links that use get to send ID

Posted 09 May 2011 - 05:33 AM

Okay this was not working as expected though it was my fault as i said use CUSTID i have fixed it htough as customerID was the reference to the ID in the table now it is sending correctly there was also a couple of little typo's though i don't mind that Thanks for Answering my Question so Promptly

Okay using the same data would i make a dropdown box using <select> ie create a form using the lastname as the option which i know <option> tag would do this then how do i assign the ID to the option value

I know the in javascript to return a for loop while <array.length to print the correct number of options

for eample

for (int i = 0; i<array.length;i++)

<option> array [i] </option>

though how would i set the value to equal the ID

otherwise value = "CustomerID"

This post has been edited by jaimesharp: 09 May 2011 - 05:37 AM

Was This Post Helpful? 0
  • +
  • -

#9 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Concatenating Queries and Creating links that use get to send ID

Posted 09 May 2011 - 05:42 AM

This is very similar to the code i posted before to the generate a table. now just assign the custID to the value of the option, like:

$str .= "<option value='".$row['custID']."'>".$row['lastName']."</option>";

Was This Post Helpful? 0
  • +
  • -

#10 jaimesharp  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 27-May 09

Re: Concatenating Queries and Creating links that use get to send ID

Posted 09 May 2011 - 05:49 AM

Awesome Thanks i Will try that



In the previous code I added the first name to the table as well

though it is returning

11111111111111} above the table how do i fix this


and thanks for your swift response
Was This Post Helpful? 0
  • +
  • -

#11 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Concatenating Queries and Creating links that use get to send ID

Posted 09 May 2011 - 05:54 AM

One thing I forgot to mention, you should insert the SELECT in FORM tags, and have a submit button (or use javascript).
In html it would be:
<form method='GET' action='example.php'>
<select>
..add options
<input type='submit'>
</form>


you'll have to post your updated code, it is hard to tell what the problem is without seeing it.
Was This Post Helpful? 0
  • +
  • -

#12 jaimesharp  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 27-May 09

Re: Concatenating Queries and Creating links that use get to send ID

Posted 09 May 2011 - 05:59 AM

I know i am close though i haven't got it completly right


<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Prac 3 Task 9</title> </head> <body>
<?php
{
    $conn = mysql_connect("localhost", "twastudent", "prac3");
    
    if (!$conn)
          {
          die('Could not connect: '. mysql_error());
          }
    
    mysql_select_db("warehouse", $conn) or die('Database not found '. mysql_error() );
    
    mysql_select_db("warehouse", $conn);

$sql = "SELECT customerID,lastName FROM customer";
     $rs = mysql_query($sql,$conn);

        {
    ?>
    
    <form method = "GET" action = 'Task8.php'>
    
    <formset>
    <?php
$str = "";
        while ($row =  mysql_fetch_assoc($rs)){
    
   $str .= "<option value='".$row['customerID']."'>".$row['lastName']."</option>";
  ?> 
<input type = "submit"> </input> <input type = "reset"></input>
    
    </formset>
    
    </form>
    
    <body>
    
    </html> 


as i am getting an error unexpected $end on line 39 which is the </html> tag im sure i am missing a closing php tag and the submit the mthod should be get to task8.php i will finish the url of for the method


Any further help is greatly appreciated


the previous code is

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Prac 3 Task 9</title> </head> <body>
<?php
{
    $conn = mysql_connect("localhost", "twastudent", "prac3");
    
    if (!$conn)
          {
          die('Could not connect: '. mysql_error());
          }
    
    mysql_select_db("warehouse", $conn) or die('Database not found '. mysql_error() );
    
    mysql_select_db("warehouse", $conn);

$sql = "SELECT customerID,firstName, lastName FROM customer";
     $rs = mysql_query($sql,$conn);

    if (mysql_num_rows($rs)>0)
        {
    ?>
        <table border="1" summary=" Customer Order Details">
        <tr>
        <th>Customer ID</th>  <th>First Name </th> <th> Last Name </th>
        </tr>
    <?php
      
$str = "";
while ($row =  mysql_fetch_assoc($rs)){
$str .= "<tr">
$str .= "<td>".$row['customerID']."</td>";
$str .= "<td>".$row ['firstName']."</td>";
/*third co, last name as a link  to example.php with custID passed with GET*/
$str .= "<td><a href='task8.php?custID=".$row['customerID']."'>".$row['lastName']."</a></td></tr>";
}
echo $str;
?> 
             } 

        </table>
<?php
        }


      
mysql_close($conn);
}
?>

This post has been edited by jaimesharp: 09 May 2011 - 06:02 AM

Was This Post Helpful? 0
  • +
  • -

#13 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Concatenating Queries and Creating links that use get to send ID

Posted 09 May 2011 - 06:05 AM

you don't have a clsoing tag for body.
<body>
</html> 

also, the option tags should be inside a SELECT tag:
<select name='customers'>
<option>...
</select>


you don't close the while loop block.
you should use:
echo $str;

after the while loop, like:
    <?php
$str = "";
        while ($row =  mysql_fetch_assoc($rs)){
    
   $str .= "<option value='".$row['customerID']."'>".$row['lastName']."</option>";
   }//close while loop
echo $str; //print the string to the screen
  ?> 

Was This Post Helpful? 0
  • +
  • -

#14 jaimesharp  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 27-May 09

Re: Concatenating Queries and Creating links that use get to send ID

Posted 09 May 2011 - 06:32 AM

Lol i forgot the Select Tag and the / on the body

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Prac 3 Task 9</title> </head> <body>
<?php
{
    $conn = mysql_connect("localhost", "twastudent", "prac3");
    
    if (!$conn)
          {
          die('Could not connect: '. mysql_error());
          }
    
    mysql_select_db("warehouse", $conn) or die('Database not found '. mysql_error() );
    
    mysql_select_db("warehouse", $conn);

$sql = "SELECT customerID,lastName FROM customer";
     $rs = mysql_query($sql,$conn);

        {
    ?>
    
    <form method = "GET" action = 'Task8.php'>
    
    <formset>
    
 	<select name='customers'>
<?php
$str = "";
        while ($row =  mysql_fetch_assoc($rs)){
    
   $str .= "<option value='".$row['customerID']."'>".$row['lastName']."</option>";
   }//close while loop
echo $str; //print the string to the screen
  ?> 

  </select>
<input type = "submit"> </input> <input type = "reset"></input>
    
    </formset>
    
    </form>
    
    </body>
    
    </html> 



I think this is closer though i am getting the same error on line 45 ie </html> have i missed something else silly
Was This Post Helpful? 0
  • +
  • -

#15 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Concatenating Queries and Creating links that use get to send ID

Posted 09 May 2011 - 06:51 AM

I am not sure what this curly braces doing here:
$rs = mysql_query($sql,$conn);

        {//this one
    ?>

It seems you have to remove it.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2