Display MySQL data when linking to new page - php

I have a php script that connects to my database and returns results: name, and price, however, now I am trying to also create a link - that opens a new php page "more_info.php" and displays the correct description that corresponds to correct name.
I have been trying to accomplish this, but I haven't been able to get it working.
while($row = mysqli_fetch_array($result)) //mysql_fetch_array bring back an object, in this case the $result of the mysql query and puts it into a variable $row
{
echo "<td align='center'>" . $row['name'] . "</td> <td align='center'> <input type='button' value='More Info'; onclick=\"window.location='?start=' . $row['name.description'] . ?more_info.php?';\"> </td>";
within the onclick, I want the new window to open up, and bring in the description that matches the name, into the more_info.php
I input that code, but obviously it isn't working. Does this look like i'm on the right path at all?

Change it like this.
echo '<table>';
while($row = mysqli_fetch_array($result))
{
echo '<tr>';
echo '<td align="center">' . $row['name'] . '</td>';
echo '<td align="center">';
echo '<input type="button" value="More Info" onclick="window.location=\'more_info.php?start=' . urlencode($row['description']) . ' \';" />';
echo '</td>';
echo '</tr>';
}
echo '</table>';
and read it in more_info.php like:
<?php
$start = (!empty($_GET['start']) ? $_GET['start'] : false);
echo '<h1>Info about: ' . $start . '</h1>';
?>

shouldn't it be:
window.location='more_info.php?start=' . $row['name.description']
an alternative to using JS triggers on buttons for navigation is you could also just use a link and css style it to look like a button <a href="more_info.php?start=' . $row['name.description'].'" class="button">

Related

Why cannot call the div #popup1?

Why cannot call the div #popup1? I want to pass the $row['staffId'] to div and call the popup to show the information .
$result = mysql_query("SELECT * FROM Staff WHERE companyId='$companyIdResult'");
echo "<div>
<table >";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td >" . $row['staffName'] . "</td>";
echo "<td>" . $row['staffPhone'] . "</td>";
echo "<td><a class ='editbutton' href=' #popup1?edit_id=".$row['staffId'] ."'>Edit</a></td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
if($_GET['edit_id'] != ""){
$staffId = $_GET['edit_id'];
$sql2 = mysql_query("SELECT *FROM Staff WHERE staffId='".$staffId."'");
echo '<div id="popup1" class="overlay" >';
echo '<div class="popup">';
echo '<input type="text" name="staffName" value= ".$row['staffName']." ><br>';
echo '<input type="text" name="staffPhone" value=".$row['staffPhone']."><br>';
echo '</div>';
echo '</div>';
}
First of all you cannot append query parameters (?edit_id=...) to an anchor like #popup1, also you seem to misunderstand how PHP and the browser interact, you cannot open a popup and create it dynamically on the server side the way you seem to plan to. You could use AJAX to load the matching form for the staffId into your popup or generate a popup for every staffId beforehand (and create matching links with different anchors), but you try to open a popup1 with values generated on the server side and that doesn't work in this way.

How do I organize data in a PHP search query?

I'm in the process of making a PHP website, where a user can input data, then search through it later, but I can't seem to organize the data :/
Heres my code:
<form action="uploads.php" method="GET"><input id="search" type="text" placeholder="Type here"><input id="submit" type="submit" value="Search"></form></body></html>
When a user searched for their name, it results as so:
firstname=Mickey lastname=Mouse item1= item2= item3= item4= item5= item6=
Is there any way I can add a CSS or something to get the entries to line break or seperate?
In Your display page while displaying the data you can use the <br> tag so that it will display each and every data in different line.
First Name: <?php echo $loopvariable['fname'].'<br />'; ?>
Last Name: <?php echo $loopvariable['lname'].'<br />'; ?>
Like this you can provide for all the data which you print.
Output:
First Name: Name One
Last Name: Name Two
And you can provide as such information using the break tags in separate lines.
Basically in PHP you can echo html code so you can echo <br> statements as in the previous answer you can also echo css and you can create a block of code that you include
<?php
include('SomeMoreCode.php');
?>
You can also echo formatting commands to create a table. Just place the html statements in'' and join html and mysql/php values with .
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>date</th> <th>Home Team</th> <th></th><th></th><th>Away Team</th> <th></th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['hometeam'] . '</td>';
echo '<td>' . $row['fthg'] . '</td>';
echo '<td>' . $row['ftag'] . '</td>';
echo '<td>' . $row['awayteam'] . '</td>';
echo '<td>Edit</td>';
echo "</tr>";
}
echo "</table>";
non mysql - php only
<?php
$date='19/6/16';
$hometeam='Man United';
$fthg='3';
$ftag='2';
$awayteam='Man City';
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>date</th> <th>Home Team</th> <th></th><th></th><th>Away Team</th> </tr>";
echo "<tr>";
echo '<td>' . $date . '</td>';
echo '<td>' . $hometeam . '</td>';
echo '<td>' . $fthg . '</td>';
echo '<td>' . $ftag . '</td>';
echo '<td>' . $awayteam . '</td>';
echo "</tr>";
echo "</table>";
?>

How to display data the corresponds to other data MySQL PHP

My working PHP script connects to my database fetches the data, and then puts it into a table like so:
while($row = mysqli_fetch_array($result)) //mysql_fetch_array bring back an object, in this case the $result of the mysql query and puts it into a variable $row
{
echo "<tr>";
echo "<td align='center'><img height='100' width='100' src=\"" . $row['picturepath'] . "\" /></td>"; //here I can use the results and call out which column I want from my database
echo '<td align="center">' . $row['name'] . '</td>';
echo '<td align="center"><input type="button" value="More Info" onclick="window.location=\'more_info.php?start=' . urlencode($row['description']) .' \';" /></td>';
echo "<td align='center'>" . $row['price'] . "</td> <td align='center'> <input type='button' value='Add to Order' onclick=''> </td>";
echo "</tr>";
}
echo "</table>";
echo "<table width=\"1024\" align=\"center\" >";
echo "<tr height=\"50\"></tr>";
The "More Info" button links to a separate php script, more_info.php, where the description is read onto the page for that specific item. I wanted to also display the image that corresponds to that item, and have been having trouble doing so.
What I have tried.
I have modified the above code to display:
more_info.php?start=' . urlencode($row['description']) . urlencode($row['picturepath']) . '\';" /></td>';
which then pulls the correct information in, however it literally pulls in the filename of the image, rather than the actual picture.
Goal
I'd like to have the "More Info" link, pull the description and image of the item the user clicks 'more info' on, and dump both into "more_info.php". Would I have to use $_POST in this scenario? Or maybe on the more_info.php run a query where the picturepath is displayed only if it matches the name of the item? I'm not sure...
tough to tell whats going on in more_info.php without seeing the code, but you need a way to differentiate the 2 fields (description, picturepath) in that php script. for the url use something like:
$url = "more_info.php?start=" . urlencode($row['description']) . "&path=" . urlencode($row['picturepath']);
use $url string in your onclick:
... onclick="window.location=\'$url\'" ...
then in the more_info.php use $_GET to retrieve the 2 parameters (start, path) passed as part of the URL:
if(isset($_GET['start']) && isset($_GET['path'])){
$description = $_GET['start'];
$picturepath = $_GET['path'];
echo "Description: $description <img src=\"$picturepath\"/>";
}
Not sure if this helps/was what you were looking for. if not provide the more_info.php and it's current output.

Coding dynamic anchor tags

I want to link an anchor tag from one page to another. The second of which is a view all page that pulls data from the database. When run the code below, the second page is loaded but doesn't move to the tag. Here is the code for the 1st page:
echo "<h2>$dd. $info and ID is
**$id**</h2><hr />";
Here is the code for the page it should point to:
while($row = mysql_fetch_array( $result )) {
**$ggg = $row['id'];**
echo "<tr>";
echo '<td><input type="button" value="Delete"></td>**<a name="$ggg"></a>**';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['info'] . '</td>';
echo "</tr>";
}
Your php variable $ggg will not be parsed as it is being echo'ed inside single quotes '' so your <a> anchor will not be correct-
echo '<td><input type="button" value="Delete"></td><a name="$ggg"></a>';
Change it to -
...<a name="'.$ggg.'"></a>...
so it is now -
echo '<td><input type="button" value="Delete"></td><a name="'.$ggg.'"></a>';

how to format a url to be live

I have been working on trying to teach myself programming and have come stuck on a simple problem ,
the line I am working with is
echo "<td>" . $row['website'] . "</td>";
only in the database {mysql} it is in plain text under the column 'website' , I have been trying to work out how to make the row website clickable for the whole table,
i have tried <href ="echo "<td>" . $row['website'] . "</td>"";
I have tried searching the web for an answer , only i dont seem to be able to phrase the question for the right results.
thank you .
I also tried
<?
$result = mysql_query("SELECT * FROM leader");
echo "<table border='1'> <tr> <th>id</th> <th>Club</th> <th>Website</th> <th>Club Badge</th> </tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>"."<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['club'] . "</td>";
echo "<td>" . $row['website' ] . "</td>";
echo "<td><a class=\"mylink\" href=\"" . $row['website'] . "\">" . $row['website'] . "</a></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
but still got Parse error: syntax error, unexpected '<' in /home/a6332763/public_html/res.php on line 29
UPDATE......... Have now got link to show in right box only its adding the sites url in-front of the links url, here is the code , minus mysqul connection .
<?php
$result = mysql_query("SELECT * FROM leader");
echo "<table border='1'>
<tr>
<th>id</th>
<th>Club</th>
<th>Website</th>
<th>Club Badge Url</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>"."<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['club'] . "</td>";
echo "<td><a class=\"mylink\" href=\"" . $row['website'] . "\">" . $row['website'] . "</a></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<html>
<body>
Link text
Click on this link to run your first PHP script.
</body>
</html>
Anchor tags (<a>) cannot contain table rows or cells. To make the entire row clickable, you must bind an onclick handler with JavaScript or wrap the contents of each individual <td> element with its own <a> tag.
If what you are looking for is a way to make a link out the area of the row where your links is do the following:
In the PHP file:
echo "<td><a class=\"mylink\" href=\"" . $row['website'] . "\">" . $row['website'] . "</a></td>";
In the CSS file:
.mylink{display:block;}
That should do it.
Fill the appropriate onclick attribute with JavaScript code to open the new location.
If you just want the a regular link inside a table cell what you want is
<td><?= $row['website'] ?></td>
If you were actually trying to make the entire table row clickable, you need to use a javascript redirect set on the table row:
<tr onClick="window.location='<?= $row['website'] ?>'">
<td><?= $row['website'] ?></td>
</tr>

Categories