Add Hyperlink with Variable to Mysql Table - php

I would like to add a url to php table along with variable.
Any one please help me solving this.
See the comment description in table for further info.
<?php
include('config.php');
$qry = "SELECT * FROM pincode_data where pinCode='641028'";
$result = mysql_query($qry) or die (mysql_error());
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
while($row=mysql_fetch_array($result))
{
echo "<tr><td>Pincode:</td> <td><a href='pinview.php?pin=" . $pinCode . "'>".$row['pinCode'] . "</td></tr>"; //I want a hyperlink here along with pin number example url: pinview.php?pin=641028
echo "<tr><td>Office Name:</td> <td>" . $row['postOffice'] . "</a></td>";
echo "<tr><td>Office Type:</td> <td>".$row['OfficeType'] . " </td></tr>";
echo "<tr><td>Contact:</td> <td>".$row['contact'] . "</td></tr>";
echo "<tr><td>Taluk:</td> <td>".$row['talukName'] . " </td></tr>";
echo "<tr><td>District:</td> <td>".$row['districtName'] . " </td></tr>";
echo "<tr><td>State:</td> <td>".$row['stateName'] . "</td></tr>";
echo "<tr><td>Postal Division:</td> <td>".$row['postalDivision'] . " </td></tr>";
echo "<tr><td>Postal Region:</td> <td>".$row['postalRegion'] . " </td></tr>";
echo "<td>Postal Circle:</td> <td>".$row['postalCircle'] . "</td> ";
}
echo "</table>";
mysql_close();
?>

Replace with this line you have to add $row['pinCode'] instead of $pinCode
echo "<tr><td>Pincode:</td> <td><a href='pinview.php?pin=" . $row['pinCode'] . "'>".$row['pinCode'] . "</td></tr>";

Related

How to create a multiple hyperlink in in single php line

I have tried below code; as you can see in bold line i want to add two hyperlink but i am not getting results.I want to create a variable hyperlink.
<?php
echo "<!DOCTYPE html>";
echo "<html>";
echo "<title> disorders </title>";
echo "<body>";
include ('menu.php');
echo "<br>";
echo "<br>";
$disorder = $_GET ['disorder'];
$sql_disorder= "SELECT * FROM `disorders raw datasets` WHERE `Name of disorder` = '$disorder' ";
if ($result_disorder = mysqli_query($conn, $sql_disorder));
{
echo "<table border='1' cellpadding='2' cellspacing='1'> ";
echo "<tr>";
echo "<th>Accession ID</th>";
echo "<th>Title</th>";
echo "<th>Abstract</th>";
echo "<th>Tissue type</th>";
echo "<th>PMID</th>";
echo "<th>Other information</th>";
echo "</tr>";
while($row_disorder = mysqli_fetch_array($result_disorder))
{
echo "<tr>";
**echo "<td><a href='https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc= href='https://www.ebi.ac.uk/arrayexpress/experiments/" . $row_disorder['Accession ID'] . " ' ' >" . $row_disorder['Accession ID'] . "</a> </td>";**
echo "<td>" . $row_disorder['Title'] . " </td>";
echo "<td>" . $row_disorder['Abstract'] . "</td>";
echo "<td>" . $row_disorder['Tissue type'] . " </td>";
echo "<td><a href='https://www.ncbi.nlm.nih.gov/pubmed/" . $row_disorder['PMID'] . " '>" . $row_disorder['PMID'] . "</a></td>";
echo "<td><a href='disorder_info.php?" . $row_disorder['Other information'] . " ' >" . $row_disorder['Other information'] . "</a></td>";
echo "</tr>";
}
echo "</table>";
}
echo "</body>";
echo "</html>";
?>
I don't know why you are using two href in a single tag, that does not support by any dom. Although replace your code with the following line.
echo "
<td><a href='https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=' href='https://www.ebi.ac.uk/arrayexpress/experiments/' " . $row_disorder['Accession ID'] . " ' '>" . $row_disorder['Accession ID'] . "</a> </td>";
You will have to use urlencode for it to work
$queryString = urlencode("href='https://www.ebi.ac.uk/arrayexpress/experiments/{$row_disorder['Accession ID']}'");
echo "
<td>
<a href='https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=$queryString'>
{$row_disorder['Accession ID']}
</a>
</td>";

passing parameter from php table row

Is it possible to pass a name_id parameter through the href via a row in a php table?
Sample Code:
<?php
$name = $_GET["name"];
$con = mysqli_connect("", "", "", "");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM table WHERE name LIKE '%$name%' ");
echo "<table class='table table-condensed'>
<tr>
<th> name id </th>
<th> name </th>
<th> link </th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name_id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . "<a href='name.php?name_id=$name_id'>view</a>" . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Eg. Multiple names with an auto incremented id is in the table and I would like it to open the name.php page where I can GET the name_id from the url?
Thanks in advance.
This worked for me.
echo "<td>" . "<a href='name.php?name_id=" . $row['name_id'] ."'>". $row['name_id'] . "</a>" . "</td>";
Hope this helps someone!
I think that this will be the simplest solution
echo "<td>" . "<a href='name.php?name_id=" . $row['name_id'] . "'>view</a>" . "</td>";

PHP output sorting

Below is a part of my PHP code where it echos the result in an array format. I'm trying to make it so it posts the result in a table. What should I do?
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "<a href=\"search_search.php?id=$ID\">" .
$FirstName . " " . $LastName . " " . $Email . "</a></li>\n";
echo "</ul>";
Do something like for html table-
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "</tr>";
}
echo "</table>";
Go back to basics, http://www.w3schools.com/php/php_mysql_select.asp
Look at Display the Result in an HTML Table.

Sort sql query & ID reset and start in numerical order every time I make a query or sort from <th> tag

I have a database called forms1 and a table named demo
The fields in the table are ID, Autore, Titolo, cit
I wish I had a first line that gives me the ability to sort alphanumeric values ​​that are retrieved by the query, such as
How can i modify my code?
This is cerca2.php:
<style>
br {margin-bottom:-10px;}
</style>
<form action="cerca2.php" method="post">
<b>Nome</b> <input type="text" name="Nome">
<b>Numero </b><input type="text" name="Numero">
<b>city </b><input type="text" name="city">
<input type="Submit">
</form>
<style>
tr:nth-of-type(odd) { background-color: AZURE; }
tr:nth-of-type(even) { background-color: CYAN; }
</style>
<style>
tr:hover{background-color<img src="images/smilies/biggrin.gif" border="0" alt="">EEPSKYBLUE;}
</style>
<?php
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='auto' bgcolor=”#7FFFD4″> <i>ID<i/></td>";
echo "<td width='auto' > <i>Nome<i/></td>";
echo "<td width='auto' ></td>";
echo "<td ></td>";
echo "</tr>";
define('DB_NAME', 'forms1');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can't use ' . DB_NAME . ': ' . mysql_error());
}
$Nome = str_replace(' ', '%', $_POST['Nome']);
$Numero = str_replace(' ', '%', $_POST['Numero']);
$city = str_replace(' ', '%', $_POST['city']);
$arNome = str_split($Nome);
$arNumero = str_split($Numero);
$arcity = str_split($city);
$Nome='';
foreach ($arNome as $value)
{
if ($value=='%') continue;
$Nome.=$value.'%';
}
$Numero='';
foreach ($arNumero as $value)
{
if ($value=='%') continue;
$Numero.=$value.'%';
}
$city='';
foreach ($arcity as $value)
{
if ($value=='%') continue;
$city.=$value.'%';
}
$sql = mysql_query("SELECT * FROM demo WHERE Autore LIKE '%$Nome%' AND Titolo LIKE '%$Numero%' AND cit LIKE '%$city%' ORDER BY Autore") or die(mysql_error());
while($row=mysql_fetch_array($sql)){
echo "<tr>";
echo "<td width='auto' bgcolor=”#FF0000 ″>" . " ". "<b>" . $row[0] . " ". "<b/>". "</td>";
echo "<td width='auto'>" . " " . $row[1] . " " . "</td>";
echo "<td width='auto'>". "</td>";
echo "<td width='auto'>" . " ". "<i>" . $row[2] . "<i/>". " " . "</td>";
echo "<td width='auto'>" . " ". "<i>" . $row[3] . "<i/>". " " . "</td>";
echo "</tr>";
}
mysql_close();
?>
I would click on Nome and other th (except ID) to reorder the values ​​alphanumeric AZ or ZA viceversa when i click. At the moment this is my result page (look code above) when i call a query:
I would, too - but I do not know how to do - that the ID value always start from 1 and continue in numerical order according to the number of x values ​​retrieved every time I make a query or sort from th tag. In my case, instead, is always dependent on the value it represents.
You see it from the picture that the numbers appear in random order.
Here, I do not want this.
Try this code-part:
$i = 0; while($row=mysql_fetch_array($sql)){
$i++;
echo "<tr>";
echo "<td width='auto' bgcolor=”#FF0000 ″>" . " ". "<b>" . $i . " ". "<b/>". "</td>";
echo "<td width='auto'>" . " " . $row[1] . " " . "</td>";
echo "<td width='auto'>". "</td>";
echo "<td width='auto'>" . " ". "<i>" . $row[2] . "<i/>". " " . "</td>";
echo "<td width='auto'>" . " ". "<i>" . $row[3] . "<i/>". " " . "</td>";
echo "</tr>";
}

Hiding a table if a search result does not exist

Exactly what the title says. I want the table containing all of the search queries to be hidden, but I've tried a lot of things and none of them work. For example, if($myData!=null) {proceed with showing the table}, but that didn't work. isset() also didn't work. Any ideas?
<style>
ul
{
list-style-type: none;
}
</style>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Search Chemicals</title>
</head>
<p>
<body>
<h3>Chemical Information</h3>
<p>You may search by Catalog number, CASRN, or the chemical name.</p>
<form method="post" action="search.php?go" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
<?php
error_reporting(0);
if (isset($_POST['submit'])) {
if (isset($_GET['go'])) {
if (preg_match("/^[a-zA-Z0-9]+/", $_POST['name'])) {
$name = $_POST['name'];
$conn = mysql_connect("localhost", "Blimeo", "password");
$db = mysql_connect("localhost", "-", "-") or die('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb = mysql_select_db("chemicals");
//-query the database table
$sql = "SELECT * FROM products WHERE Catalog LIKE '%" . $name . "%' OR CASRN LIKE '%" . $name . "%' OR Chemical_Name LIKE '%" . $name . "%'";
$myData = mysql_query($sql, $conn);
echo "<table border=1>
<tr>
<th>Catalog</th>
<th>Image</th>
<th>CASRN</th>
<th>Chemical Name</th>
<th>Quantity 1</th>
<th>Price 1</th>
<th>Quantity 2</th>
<th>Price 2</th>
<th>Quantity 3</th>
<th>Price 3</th>
<th>Quantity 4</th>
<th>Price 4</th>
</tr>";
while ($record = mysql_fetch_array($myData)) {
echo "<tr>";
echo "<td>" . $record['Catalog'] . "</td>";
echo "<td><img src=\"./img/" . $record['Image'] . "\" alt=\"Chemical\"/></td>";
echo "<td>" . $record['CASRN'] . "</td>";
echo "<td>" . $record['Chemical_Name'] . "</td>";
echo "<td>" . $record['Quantity1'] . "</td>";
echo "<td>" . $record['Price1'] . "</td>";
echo "<td>" . $record['Quantity2'] . "</td>";
echo "<td>" . $record['Price2'] . "</td>";
echo "<td>" . $record['Quantity3'] . "</td>";
echo "<td>" . $record['Price3'] . "</td>";
echo "<td>" . $record['Quantity4'] . "</td>";
echo "<td>" . $record['Price4'] . "</td>";
echo "</tr>";
echo "</form>";
echo "<ul>\n";
echo "<li>" . "" . $Catalog . " " . $CASRN . " " . $Chemical_Name . "</li>\n";
echo "</ul>";
}
}
} else {
echo "<p>Product not found! Please rephrase your search criteria.</p>";
}
}
?>
</body>
</html>
</p>
You should add mysql_num_rows();
$myData = mysql_query($sql, $conn);
$exists = mysql_num_rows($myData);
if($exists) {
echo "<table border=1>";
//..................
echo "</table>";
} else {
echo "<p>Product not found! Please rephrase your search criteria.</p>";
}
Well, it seems you are echoing out your table regardless of the results of the search query. You should probably check the number of rows returned in teh result set and only echo out the table if the count is > 0.
Use <div visibility="hidden"> to hide the table and use Javascript to change the visibility based on the search queries or some other condition.

Categories