Page 1 of 1

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

Posted: Thu Jan 06, 2011 11:56 pm
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.

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

Posted: Fri Jan 07, 2011 12:48 am
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
}
?>

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

Posted: Fri Jan 07, 2011 1:04 am
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.

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

Posted: Fri Jan 07, 2011 1:14 am
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'];
}
?>

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

Posted: Fri Jan 07, 2011 1:18 am
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.

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

Posted: Fri Jan 07, 2011 8:05 am
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%' ");
?>

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

Posted: Sun Jan 09, 2011 10:02 pm
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.

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

Posted: Mon Jan 10, 2011 6:25 am
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?