how to make echo PHP link - php

I have a table called as feedback and the table look like this;
UserName image
abc image/x.jpeg
def image/y.png
I want to make a link in php by using echo.
my coding as I try is ,
<?php
require_once('Connections/localhost.php');
mysql_connect("localhost","root","");
mysql_select_db("iis");
$result= mysql_query("SELECT * FROM feedback") or die (mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo ''.$row['UserName'].'<br />';
}
?>
I want results in a form as below,
abc = image/x.jpeg
def = image/y.png
so that when you click on the link, it will direct to the folder image and show the image stored by each person.

Suggest you to do this.
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr><td>'.$row['UserName'].' </td><td>=</td><td>'.$row['image'].'</td></tr>';
}
echo '</table>';

Related

How can I add an Sql Query in the middle of HTML code?

I want to make a login system, where the user can see all the messages he has received. In my database the insurance_company is the username and that is what I want it to show by. Here is my code. How do I fix it?
- Thanks Prem
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo $row['Claim'] . "<br />";
}
?>
PHP code can be written anywhere in HTML code. You need to just place your PHP code between tags. As you can see in the example bellow I have used php code between p tags.
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {?>
<p> <?php echo $row['Claim'] . "<br />"; ?> <p>
<?php }
?>
If you want it to show insurance_company, you must echo this value in the response array:
echo $row['insurance_company']

Calling from a MySQL/PHP array multiple times in a page

I'm probably asking a very simple question here - I know the basics of calling an array but I think I'm probably not doing it in the most efficient way... I'm calling some data into an array at the start of my page and then I want to be able to use this data-set multiple times throughout the page without wrapping everything in PHP if possible.
At present I'm doing it like this -
A variable ('video') is passed to my page through the URL which I get like so:
<?php
$video = $_GET['video'];
?>
My <title> tag is pulled from the selected database (also titled 'video')
<?php
$title = mysql_query("SELECT * FROM video WHERE ID = '{$video}'") or die(mysql_error());
mysql_real_escape_string($video);
while($head = mysql_fetch_array( $title )) {
echo "{$head['title']} - BY XXXXX</title>";
echo "<meta property=\"og:title\" content=\"{$head['title']} - BY XXXX\"/>";
}
?>
I then want to use the {$video} data later on the same page, but defining a slightly different variable like so:
<?php
$data = mysql_query("SELECT * FROM video WHERE ID = '{$video}' ORDER BY added DESC") or die(mysql_error());
mysql_real_escape_string($video);
while($info = mysql_fetch_array( $data )) if ($info['ytembed'] == 'yes') {
echo "{$info['embedcode']}";
echo "<div class=\"videobox1\">";
echo "<div class='video-title'>{$info['title']}</div>";
echo "<div class='video-subtitle'>{$info['subtitle']}</div>";
echo "<div class='video-credits'>{$info['cast']}</div>";
echo "<div class='back'>«back</div></div>";
} else {
echo "no embed code";
}
?>
So at the moment every time I want to pull from that data I'm calling the whole array again - it would be amazing if instead of doing this I could just print/echo selected items
Is there a way to make my code more efficient and do this?
I'm also looking to Validate the ID and if it doesn't exist within the video DB send the user to a 404 page - but perhaps that's a separate question.
Hello this is refined code
Replace first 1 with this.
$video = $_GET['video'];
$video = mysql_real_escape_string($video);
$videodata = mysql_query("SELECT * FROM video WHERE ID = '{$video}' LIMIT 1") or die(mysql_error());
// execute the query and check if video id exist or not
if(mysql_num_rows($videodata) == 0){
// 404 redirect code.
}
Replace Second with
$videodataArray = array(); // created array for storing video data
while ($head = mysql_fetch_array($videodata))
{
$videodataArray = $head ; // store the value in video data array for to use in fulll page
echo "{$videodataArray['title']} - BY XXXXX</title>";
echo "<meta property=\"og:title\" content=\"{$videodataArray['title']} - BY XXXX\"/>";
}
Replace last one with
echo "{$videodataArray['embedcode']}";
echo "<div class=\"videobox1\">";
echo "<div class='video-title'>{$videodataArray['title']}</div>";
echo "<div class='video-subtitle'>{$videodataArray['subtitle']}</div>";
echo "<div class='video-credits'>{$videodataArray['cast']}</div>";
echo "<div class='back'>«back</div></div>";

Query on PHP, web development

I have made a connection to mysql database and echoing values from a table.
while($data = mysql_fetch_array($res))
{
?>
<a href="nextpage.php"<?php echo $data['rowname'] ?></a>
<?php
}
?>
Problem is when I click on the particular link, on the nextpage.php, it should display only the result of the value of a href clicked. So on the nextpage.php, I have defined something like SELECT * from tablename where rowname = 'a href value'.
What's happening now is that it displays only the last rowname value regardless of whichever link I click on, very obvious!
I have tried forms, arrays, SESSIONS but to no avail. How do I fix this?
the href should be like this
<?php echo $data['rowname']; ?>
and then on next page you can use $_GET['val'] and pass it to SELECT query
Try example as below
page1.php
while($data = mysql_fetch_array($res))
{
echo "<a href='nextpage.php?id=".$data['rowname']." >". $data['rowname'] ."</a>";
}
?>
your href will look like nextpage.php?id=[your value]
nextpage.php
$qry = mysql_query("select * from [table_name] where id = ".$_GET['id']."",$con);
while($data = mysql_fetch_array($res))
{
echo $data[0];
}
on nextpage pass value using $_GET['id'] to sql query.

How to determine what ID has been clicked to display relevant information? PHP / SQL

I currently have this code which displays all required information on the page:
$sql = "select * from livecalls ORDER BY Completion_Date ";
$query = mysql_query( $sql );
while( $row = mysql_fetch_assoc($query) )
{
echo "<tr><td>$row[ID]</td>";
echo "<td>$row[Type]</td>";
echo "<td>$row[VNC_Number]</td>";
echo "<td>$row[Completion_Date]</td>";
echo "<td>$row[Logged_By]</td></tr>";
}
echo "</table>";
This works fine, however I am wanting to be able to click the $row[ID] section to open a new window and display the $row[Problem] which is related to that ID number..
I'm struggling to think how to get the ID information across to a new page to be able to search for the right Problem information to display and the code to do this?
Any help would be appreciated.
You can use a anchor tag for this purpose like
echo "<tr><td><a href='yourpage.php?id=".$row[ID]."'>".$row[ID]."</a></td>";
and in new page, you can use
$_GET['id'] for getting the id.
Also while echoing html along with php variable try to do like this:
while( $row = mysql_fetch_assoc($query) )
{
echo "<tr><td><a href='yourpage.php'>".$row['ID']."</a></td>";
echo "<td>".$row['Type']."</td>";
echo "<td>".$row['VNC_Number']."</td>";
echo "<td>".$row['Completion_Date']."</td>";
echo "<td>".$row['Logged_By']."</td></tr>";
}
I guess your not using any php framework (ex. codeigniter). Because if you do,this is easy using routing.
The very basic solution is Passing variables in a URL
http://html.net/tutorials/php/lesson10.php
Then if you want it to open in a new window instead of current window use
Use _blank in your a href
Visit W3Schools!
$sql = "select * from livecalls ORDER BY Completion_Date ";
$query = mysql_query( $sql );
while( $row = mysql_fetch_assoc($query) )
{
echo "<tr><td><a href='your_url/?id=".$row[ID]."'>$row[ID]</a>`enter code here`</td>";
echo "<td>$row[Type]</td>";
..
...
....

get one variable out of database with PHP

Ok obviously i'm a beginner.
What i'm trying to do here is probably simple.
I'm trying to get all the field of my database to echo on my website. So i use this to show only the basic details:
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("crnew") or die(mysql_error());
$result = mysql_query("SELECT * FROM releases")
or die(mysql_error());
while($row = mysql_fetch_array( $result )
) {
// Print out the contents of each row into a table
echo '<ul class="releaselist">';
echo '<li>';
echo $row['products_name'];
echo '</li>';
echo '<li>';
echo $row['products_title'];
echo '</li>';
echo '<li>';
echo '<img class="releaseimg" src="'.$row['products_image'].'">';
echo '</li>';
echo '</ul>';
}
?>
So far eveything works fine.
What i want to do with this is get my URL to look like this when i click on a single image.
www.mywebsite.com/detailed.php?id=1
and show everything available in the table RELEASES
The way i've done it which is not working is:
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("crnew") or die(mysql_error());
$result = mysql_query("SELECT * FROM releases
WHERE products_id=products_id")
or die(mysql_error());
if ($result){
$row = mysql_fetch_array($result);
echo $_GET['products_id'];
}
?>
If I'm understanding you correctly, your first script needs to be updated to:
echo '';
That will turn your image into a link. At that point your second script should be able to use:
_GET['id']
to retrieve this value and use it in your query. Thusly:
$result = mysql_query("SELECT * FROM releases
WHERE products_id=" . mysql_real_escape_string($_GET['id']));
Now if you really want to products_id in the URL instead of id, just change it 'id' to products_id in the echo line in the first script and all of the $_GET uses in the second script.
Try changing this line:
echo '<img class="releaseimg" src="'.$row['products_image'].'">';
To this one:
echo "<a href='detailed.php?products_id=".$row['products_id']."'><img class='releaseimg' src='".$row['products_image']."'></a>";
Hmm try using $row['products_id'] instead of $_GET['products_id']; $_GET is used when accessing a webpage and the reason that $row['products_image'] worked is because you accessed the database results.
You have misplaced the use of products_id...
Your code should like something like this...
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("crnew") or die(mysql_error());
$result = mysql_query("SELECT * FROM releases
WHERE products_id='".intval($_GET['id'])."'")
or die(mysql_error());
if ($result){
$row = mysql_fetch_array($result);
echo $row['products_id'];
}
?>

Categories