how to apply hyperlink to first element in each row of table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ishakya
Forum Commoner
Posts: 40
Joined: Tue Jan 04, 2011 4:58 am

how to apply hyperlink to first element in each row of table

Post by ishakya »

Hello,
I need to apply a hyperlink for all first element in each row of a table which is dynamically retrieved.
Assume:Select query returns 8 rows & 2 columns.But rows & columns will be changed according to the query.So my problem is ,i need to apply a hyperlink to data in first column.So can u help me to do that it will be very grateful.
This is my code....

Code: Select all

if (isset($_POST['search'])){
	/*{print_r($_POST);}*/ 
			
	 
	  $tx=$_POST['sea'];
	
 
	
//$result =mysql_query("select * from strdet where taxt ='$tx' || odrno ='$tx'");
//$query = "select field1, fieldn from table [where clause][group by clause][order by clause][limit clause]";
//$output=mysql_query("Select * from strdet

$result =mysql_query("select * from strdet where styid  LIKE '%$tx%' || styname LIKE '%$tx%' || odrno ='$tx' ||  odrdet LIKE '%$tx%' || date LIKE '%$tx%' || merch LIKE '%$tx%' || gen LIKE '%$tx%' || taxt  LIKE  '%$tx%' ");
if (($result)||(mysql_errno == 0))
{
  echo "<table width='100%' border='0' cellspacing='1'><tr>";
  if (mysql_num_rows($result)>0)
  {
          //loop thru the field names to print the correct headers
          $i = 0;
          while ($i < mysql_num_fields($result))
          {
       echo "<th>". mysql_field_name($result, $i) . "</th>";
       $i++;
    }
	echo"$i";
    echo "</tr>";
   /*$myQuery = "show columns from strdet";
$result = mysql_query($myQuery);
$row = mysql_fetch_row($result);
$columnCount = count($row); 
echo"$columnCount";*/
    //display the data
    while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
    {
      echo "<tr>";
      foreach ($rows as $data)
      {
   [b]  echo '<td align="center"><a href='. $data .'>'. $data .'</a>';[/b]
      }
    }
  }else{
    echo "<td align='center'>No Results found</td></tr>";
  }

  echo "</table>";
}else{
  echo "Error in running query :". mysql_error();
}
}
?>

bolded part will apply hyperlink to all data in retreived data...
Hope i explained everything in my mind.So please be kind enough to give a help.
Thank you.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: how to apply hyperlink to first element in each row of t

Post by social_experiment »

Inside this while loop you use a for loop.

Code: Select all

<?php
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC)) {
$j = 1; 
 //
 for ($i = 0; $i < $j; $i++) {
  // create hyperlink here
 }
  // echo rest of data
}
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
ishakya
Forum Commoner
Posts: 40
Joined: Tue Jan 04, 2011 4:58 am

Re: how to apply hyperlink to first element in each row of t

Post by ishakya »

Thanks social experiment, i tried it.
But it gives an error.
Says:Undefined variable in line 71;
Line 71 is:

Code: Select all

echo '<td align="center"><a href='. $data .'>'. $data .'</a>';
 }
  echo '<td align="center"><a href='. $data .'>'. $data .'</a>';


Here is the code i tried:

Code: Select all

if (isset($_POST['search'])){
	/*{print_r($_POST);}*/ 
			
	 
	  $tx=$_POST['sea'];
	
 
	
//$result =mysql_query("select * from strdet where taxt ='$tx' || odrno ='$tx'");
//$query = "select field1, fieldn from table [where clause][group by clause][order by clause][limit clause]";
//$output=mysql_query("Select * from strdet

$result =mysql_query("select * from strdet where styid  LIKE '%$tx%' || styname LIKE '%$tx%' || odrno ='$tx' ||  odrdet LIKE '%$tx%' || date LIKE '%$tx%' || merch LIKE '%$tx%' || gen LIKE '%$tx%' || taxt  LIKE  '%$tx%' ");
if (($result)||(mysql_errno == 0))
{
  echo "<table width='100%' border='0' cellspacing='1'><tr>";
  if (mysql_num_rows($result)>0)
  {
          //loop thru the field names to print the correct headers
          $i = 0;
          while ($i < mysql_num_fields($result))
          {
       echo "<th>". mysql_field_name($result, $i) . "</th>";
       $i++;
    }
	echo"$i";
    echo "</tr>";
   /*$myQuery = "show columns from strdet";
$result = mysql_query($myQuery);
$row = mysql_fetch_row($result);
$columnCount = count($row); 
echo"$columnCount";*/
    //display the data
    while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
    {
      echo "<tr>";
	  //while ($rows = mysql_fetch_array($result,MYSQL_ASSOC)) {
$j = 1;
 //
 for ($i = 0; $i < $j; $i++) {
  echo '<td align="center"><a href='. $data .'>'. $data .'</a>';
 }
  echo '<td align="center"><a href='. $data .'>'. $data .'</a>';
//}
      /*foreach ($rows as $data)
      {
       echo '<td align="center"><a href='. $data .'>'. $data .'</a>';
      }*/
    }
  }else{
    echo "<td align='center'>No Results found</td></tr>";
  }

  echo "</table>";
}else{
  echo "Error in running query :". mysql_error();
}
}
if there are any mistakes can u help me?.
Because i'm new to php.
thanks again for your help.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: how to apply hyperlink to first element in each row of t

Post by social_experiment »

Inside the while loop you have to echo values returned from the array.

Code: Select all

<?php
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC)) {
$j = 1; 
 //
 for ($i = 0; $i < $j; $i++) {
  echo $rows['field1'];
  echo $rows['field2'];
  }
  // echo rest of data
  echo $rows['field1'];
  echo $rows['field2'];
}
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
ishakya
Forum Commoner
Posts: 40
Joined: Tue Jan 04, 2011 4:58 am

Re: how to apply hyperlink to first element in each row of t

Post by ishakya »

:roll:
Field 1 & field 2 means??????

need some help!!!!!

can u do it my code?
My select query will display all the fields of my table.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: how to apply hyperlink to first element in each row of t

Post by social_experiment »

Field1 and 2 will be substituted with which ever fields you are selecting in this query.

Code: Select all

<?php
$result =mysql_query("select * from strdet where styid  LIKE '%$tx%' || styname LIKE '%$tx%' || odrno ='$tx' ||  odrdet LIKE '%$tx%' || date LIKE '%$tx%' || merch LIKE '%$tx%' || gen LIKE '%$tx%' || taxt  LIKE  '%$tx%' ");
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
ishakya
Forum Commoner
Posts: 40
Joined: Tue Jan 04, 2011 4:58 am

Re: how to apply hyperlink to first element in each row of t

Post by ishakya »

Actually thanks for your reply.
But it confused me a lot.So can u be more specific....
I couldn't understand.so can u edit it in my code?.If u do it it will be very grateful.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: how to apply hyperlink to first element in each row of t

Post by social_experiment »

ishakya wrote:so can u edit it in my code?
Actually i couldn't edit your code because i don't know what fields you want to use. What fields are in the table because if you use the asterisk in a SELECT statement (SELECT * FROM table) it means select ALL fields. If you had 20 fields, it would select all 20. What fields are in your table?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply