I could use some help here, because I cannot figure out how to echo a php row (a URL) as a link to an image.
This is the code I already have
echo"<table border=1>
<tr>
<th width=80px>Release</th>
<th width=250px>Title</th>
<th width=200px>Genre</th>
<th width=200px>Developer</th>
<th width=200px>Publisher</th>
<th width=100px>Links</th>
</tr>";
while($row=mysqli_fetch_assoc($result))
{
echo"
<tr>
<td>{$row['game_release']}</td>
<td>{$row['game_name']}</td>
<td>";
$query="SELECT * FROM genre WHERE genre_id=".$row['game_genrea'];
$genrearesult=mysqli_query($link,$query); $genrearow=mysqli_fetch_assoc($genrearesult);
echo $genrearow['genre_name'];
echo "</td>
<td>{$row['game_dev']}</td>
<td>{$row['game_pub']}</td>
<td>{$row['game_site']}</td>
</tr>";
}
echo"</table>";
The last tabel data ($row['game_site']} echoes the URL right now, but I would like to have an image appear(ie a browser icon) that links to to the echoed website and which is saved in my images folder. Is that possible? I tried different syntaxes, but nothing seems to work.
The data being executed is a url, you will have to use some html to make it the href of an anchor tag and then provide an image inside the anchor tag to get what you want.
<td><img src=\"IMAGE_URL_GOES_HERE\" /></td>
Something like this should work:
echo "<td><a href='".$row['game_site']."'> <img src='image.gif'/></a></td>";
note the "a" tag with href
Related
My code is this:
<table class='table'>
<thead>
<tr>
<th>name</th>
</tr>
</thead>
<tbody>
<?php
foreach ($stores as $store){
echo '<tr> <td>'.$store[0].'</td> </tr>';
}
?>
</tbody>
</table>
The Table and the links works perffectly, shows all the stores in a table and you can press anyone and you will be redirect to store_information, but when I press the links I need to work with $store_name in the other page to show some specific data. I think that my problem is with the line echo '<tr> <td>'.$store[0].'</td> </tr>';. I need that every link send a different information. If I code the next in $store_information:
<?php
echo $_GET['store_name'];
?>
Prints me in the page:
$store
I need to print the name of the store which I select the link. For example: petstore
You forgot to concatenate the actual PHP variable into the URL you're generating. Instead you're passing some literal text.
Replace
?store_name=store[0]">
with
?store_name='.$store[0].'">
and it should work better.
I'm in the process of building an entertainment website. It uses THREE MySQL tables; one called title, one called clips and one called dialogue.
I've been playing around all day with having my PHP fetch data from TWO of the tables (clips and dialogue) and output the content in a HTML table.
I've not had much luck and to cap it all, I was using a free host which has now reached its EP limit and although I've upgraded, I've got to wait 24 hours to try the code I've now come up with.
My question is, have I done it right? Will this collect basic information about the clip and then produce each line of the script in a new TR before going back to the start and collecting information for the next clip?
I really hope this makes sense.
I've tried researching this and have re-built my PHP from the ground up, ensuring that I annotate each section. Last time I checked, it still didn't work!
<table class='container'>";
##############################
# CLIPS QUERY & ECHOING HERE #
##############################
$clipsquery = "SELECT * FROM clips WHERE titleid=$mainurn ORDER BY clipid";
$result2 = mysqli_query($cxn, $clipsquery);
while ($row2 = mysqli_fetch_assoc($result2))
{extract ($row2);
echo "
<tr>
<td colspan='3' class='divider'></td>
</tr>
<tr>
<td class='description'>";
if ($epident == "")
{echo "";}
else
{echo "<span class='episode'>$epident</span>";}
echo "</td>
<td rowspan='2' style='text-align: right'><audio controls>
<source src='media/$clipid.mp3' type='audio/mpeg'>Your browser does not support the audio element.</audio></td>
<td rowspan='2' style='text-align: right'><a href='media/$clipid.mp3' download='$clipid.mp3'><img src='graphics/dl-icon.png' alt='Download Icon' title='Right-click here to download this clip to your computer.'></a></td>
</tr>
<tr>
<td class='description'>$clipsynopsis</td>
</tr>
<tr>
<td colspan='3'></td>
</tr>";
#################################
# DIALOGUE QUERY & ECHOING HERE #
#################################
$dialoguequery = "SELECT * FROM dialogue WHERE clipid=$clipid ORDER BY linenum";
$result3 = mysqli_query($cxn, $dialoguequery);
while ($row3 = mysqli_fetch_assoc($result3))
{extract ($row3);
echo "
<tr>
<td class='speaker'>$speaker:</td>
<td colspan='2' class='script'>$dialogue:</td>
</tr>";}}
echo "
</table>
I've got the site to work (sort of) but the formatting went wild and sometimes included clips from other sources not meant to be on the page!
There are some things I would change in your code. If you trust the variables going into your sql query then change your while loops:
while($row = $result->fetch_assoc()){
$fetchValue = $row['COLUMN_NAME'];
}
I would say you shouldn't be using extract here.
If the user is able to modify the variables going into your sql statement you should implement prepared statements. I would build a class/function and just call it when you need it.
From the HTML/CSS side its probably going to serve you better to use css div tables, especially if you are planning to make the site responsive down the line. This can convert yours
With regards to the queries, I'm not sure I understand the structure of your tables. If you want to keep playing around on your machine install a L/W/M/AMP stack depending on your operating system. See here for more information.
You only need one table where you store the data of clips.
First, you fetch the data into an array, then
you can loop through that array with foreach,
creating the table and filling it with data.
You can use an alternative syntax for foreach to make mixing PHP with HTML easier.
//Creating the array of clips
$clips = [];
$sql = "SELECT * FROM clips";
if($result = mysqli_query($db, $sql)) {
while($row = mysqli_fetch_assoc($result)) {
array_push($clips, $row);
}
}
?>
<!-- Looping through the array and creating the table -->
<table class="container">
<?php foreach ($clips as $clip): ?>
<tr>
<td colspan='3' class='divider'></td>
</tr>
<tr>
<td class='description'>
<span class='episode'><?php echo $clip["id"]; ?></span>
</td>
<td rowspan='2' style='text-align: right'>
<audio controls>
<source src='<?php echo "media/". $clip["id"]. ".mp3"; ?>' type='audio/mpeg'>
Your browser does not support the audio element.
</audio>
</td>
<td rowspan='2' style='text-align: right'>
<a href='<?php echo "media/". $clip["id"]. ".mp3"; ?>' download='<?php echo $clip["id"]. ".mp3"; ?>'>
<img src='graphics/dl-icon.png' alt='Download Icon' title='Right-click here to download this clip to your computer.'>
</a>
</td>
</tr>
<tr>
<td class='description'>
<?php echo $clip["sypnosis"]; ?>
</td>
</tr>
<tr>
<td class='speaker'>
<?php echo $clip["speaker"]; ?>
</td>
<td colspan='2' class='script'><?php echo $clip["dialogue"]; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
You need 1 MySQL table (clips) with the 4 attributes.
You should consider using PDO over mysqli, because it's a better way of dealing with the database.
Also, template engines like Smarty can help you make the code more readable and modifiable, separating the application logic from the presentation logic.
you must use JOIN in your Sql query
SELECT
clips.clipid, clips.columnname,
dilogue.id, dialogue.columnname
FROM
clips JOIN dialogue ON
clips.clipid = dialogue.id
WHERE
clipid=$clipid
ORDER BY linenum
then you can use
while($row = mysqli_fetch_assoc($result)){
echo $row['column name in clips table'];
echo $row['column name in dialogue table'];
}
but you must use Prepared statements or
mysqli_real_escape_string()
to avoid Sql Injections
also open and close PHP tags before and after html)
I need to pass value with href inside fetch array echo...
php code
$user = $get['username'];
$resource=mysqli_query($con,$sql);
echo "<font color=\"#000000\">
<h2 align=\"center\"></h2>
<table align=\"center\" border=\"1\" width=\"50%\">
<tr>
<td><b>GROUP NAME</b></td> <td><b>TASK TITLE 1</b></td> <td><b>TASK TITLE 2</b></td> <td><b>CREATED BY</b></td> <td><b>ASSIGNED TO</b></td> <td><b>DUE DATE</b></td> <td><b>PRIORITY</b></td> <td><b>CHANGE</b></td></tr> ";
while($result=mysqli_fetch_array($resource))
{
echo "<tr><td>".$result[0]."</td> <td>".$result[1]."</td> <td>".$result[2]."</td> <td>".$result[3]."</td> <td>".$result[4]."</td> <td>".$result[5]."</td> <td>".$result[6]."</td> <td> "<a href="changetask1.php?username='.$user.'">"</td></tr>";
} echo "</table></font>";
I need to pass username to next page with href value.
You are all messed up with quotes...
First... Learn to indent your code to make it readable.
Then, avoid echoing simple HTML if not necessary:
See your code after my «formatting»:
Try it, i've done a couple corrections...
<?php
$user=$get['username'];
$resource=mysqli_query($con,$sql);
?>
<!-- This is only HTML -->
<font color="#000000">
<h2 align="center"></h2>
<table align="center" border="1" width="50%">
<tr>
<td><b>GROUP NAME</b></td>
<td><b>TASK TITLE 1</b></td>
<td><b>TASK TITLE 2</b></td>
<td><b>CREATED BY</b></td>
<td><b>ASSIGNED TO</b></td>
<td><b>DUE DATE</b></td>
<td><b>PRIORITY</b></td>
<td><b>CHANGE</b></td>
</tr>
<?php
// This is a PHP block until the next ?>
while($result=mysqli_fetch_array($resource)){
echo "<tr>
<td>".$result[0]."</td>
<td>".$result[1]."</td>
<td>".$result[2]."</td>
<td>".$result[3]."</td>
<td>".$result[4]."</td>
<td>".$result[5]."</td>
<td>".$result[6]."</td>
<td><a href='changetask1.php?username=".$user."'></td>
</tr>";
}
?>
</table>
</font>
To avoid these problems of double and single quote you can write your code like following.
<td><a href="cheangetask1.php?username=<?php echo $user;?>"></td>
Now on the changetask1.php you can get username like this.
<?php echo $_REQUEST['username'];?>
I have a large dataset from MySQL that I am displaying using PHP. If the user tries to print it, it has some 50 pages of data.
The entire data is displayed using tables and simple PHP echo-s that display the data in the table fields.
What I need is - when a user tries to print it OR export to PDF in Safari (Mac), the table headers should appear on all pages. How can this be done?
Thanks
Edit: I have enclosed the table headers under but it doesnt work. It's a plain HTML page with no scripts that just displays data from database.
Code
echo "<table border='1'>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th colspan=13>Item Info</th>
<th colspan=4>Lot Info</th>
</tr>
<tr>
<th bgcolor='#C8E3FF'>S.No.</th>
<th bgcolor='#C8E3FF'>Party Name</th>
<th bgcolor='#C8E3FF'>Item No.</th>
<th bgcolor='#C8E3FF'>Date</th>
<th>Flower</th>
<th bgcolor='#C8E3FF'>Lot No.</th>
<th bgcolor='#C8E3FF'>Avg Wt.</th>
<th bgcolor='#C8E3FF'>Total Weight</th>
<th bgcolor='#C8E3FF'>Detail Page</th>
</tr></thead><tfoot></tfoot><tbody>";
echo "<tr>";
echo "<td bgcolor='#C8E3FF' rowspan=".$itemCounter.">".$sno."</td>";
echo "<td bgcolor='#C8E3FF' rowspan=".$itemCounter.">".$row['partyName']."</td>";
while($row2 = mysqli_fetch_array($itemquery))
{
$itemNo++;
echo "<td bgcolor='#C8E3FF'>".$itemNo."</td>";
echo "<td bgcolor='#C8E3FF'>".$row2['date']."</td>";
//$flower = $flower + $row2['flower'];
echo "<td>".$row2['flower']."</td>";
echo "<td bgcolor='#C8E3FF'>".$row2['lotno']."</td>";
echo "<td bgcolor='#C8E3FF'>".$row2['avgwt']."</td>";
if ($row2['totalWeight'] == 0)
{
echo "<td bgcolor='#C8E3FF'> </td>";
}
else{
$totalWt = $totalWt + $row2['totalWeight'];
echo "<td bgcolor='#C8E3FF'>".$row2['totalWeight']."</td>";
}
echo "<td bgcolor='#C8E3FF'>".$row2['detailPage']."</td>";
echo "</tr>";
}
echo "<td bgcolor='#C8E3FF'><b>Total</b></td>";
echo "<td><b>".$flower."</b></td>";
echo "<td bgcolor='#C8E3FF'></td>";
echo "<td bgcolor='#C8E3FF'></td>";
echo "<td bgcolor='#C8E3FF'><b>".$totalWt."</b></td>";
echo "<td bgcolor='#C8E3FF'></td>";
echo "</tr>";
echo "<tr></tr><tr></tr><tr></tr><tr></tr><tr></tr>";
}
echo "</tbody></table>";
As far as I know, if you properly use the THEAD section in the table, that header will be repeated on each page.
http://www.w3.org/TR/html4/struct/tables.html#h-11.2.3
added from http://www.w3schools.com/tags/tag_thead.asp:
Definition and Usage
The tag is used to group header content in an HTML table.
The element is used in conjunction with the and elements to specify each part of a table (header, body, footer).
Browsers can use these elements to enable scrolling of the table body independently of the header and footer. Also, when printing a large table that spans multiple pages, these elements can enable the table header and footer to be printed at the top and bottom of each page.
The tag must be used in the following context: As a child of a element, after any , and elements, and before any , , and elements.
I have PHP code that creates a table with values from my DB. But when i try to Alert the table in JQuery it displays the html tags aswell as the correct values.
Can someone tell me how to stop the tags from being displayed.
Hers the code for the table
echo "<table border='1' width='150' cellpadding='0' cellspacing='0'>";
echo"<tr><th>Stats</th><th>name</th></tr> ";
echo "<tr><td>var</td> <td>{$var} </td></tr>";
echo "<tr><td>var1</td> <td> {$var1} </td></tr>";
echo "<tr><td>var2</td> <td> {$var2}</td> </tr>";
echo "<tr><td>var3</td> <td>{$var3} </td></tr>";
echo "<tr><td>var4 </td> <td>{$var4 }</td></tr>";
echo "<tr><td>var5</td> <td>{$var5} </td></tr>";
echo "<tr><td>var6</td> <td>{$var6} </td></tr>";
echo "</table>"
thanks, heres the jquery
$('.but').live('click', function()
{
$.post('display.php',function(output)
{
alert(output);
});
});
THis is what is getting shown in the alert
<table border='1' width='150' cellpadding='0' cellspacing='0'><tr><th>Stats</th>
<th>name</th></tr> <tr><td>name</td> <td>0 </td></tr><tr><td>var1</td> <td> 2 </td>
</tr><tr><td>var2</td> <td> 90</td> </tr><tr><td>var3 </td> <td>0 </td></tr><tr>
<td>var 4 </td> <td>2</td></tr><tr><td>var5</td> <td>0 </td></tr><tr><td>var6</td>
<td>0 </td></tr></table>
I want table shown in the alert , not the above ,
when you alert something it tries to covert the value toString thats why the output is not rendered as HTML table as it is in the browser. If you want to display the out put use some modal plugin like the jquery UI modal.
DEMO
If you want to display html in an alert as text, you would need to parse the text out of each cell and format it into a text string using "\n" as line breaks.
A simpler solution would be to use one of the many alert plugins that allow use of either html or text with line breaks such as :
http://labs.abeautifulsite.net/archived/jquery-alerts/demo/
Or return formatted text from server if all you want is a broser default alert.