search results as links to a php page - php

for the sake of this question, consider how ebay links the results of a search to a more detailed description of an auction through the name and image of a less detailed relevant auction table.
im guessing this would require the name and image to be a hyperlink to the new php page, but im not sure how i can pass a php variable through the hyperlink so that the new php page can fetch details related to the item that was clicked.
so far ive got the php script to look like this:
<tr class = "resultindex">
<input type="hidden" value="<?php echo $ID; ?>" />
<td class = "imgholder"><?php echo $img; ?></td>
<td class = "infoholder">
<div style ="height:4px;"></div>
<div class = "infodiv"><?php echo $name; ?></div>
<div class = "locdiv"></div>
<div class = "userdiv"><span class="fromuser">From user: </span><br/><?php echo $owner; ?></div>
</td>
where the php variables are fields fetched from a mysql database. i want to be able to pass the hidden input $ID through the hyperlink, so the new php page can retrieve the info from mysql again using it as a reference, and populate a more detailed information page
how might this be done?

You can use a hyperlink combined with some GET functionality to achieve what you want like this:
$id=4; // assume ID of some item you want to link to
$href="<a href='somePHPPage.php?myID=".$id."'>some text</a>";
echo $href; // will output the hyperlink in your page.
Then in the page you can query the data that is sent like this:
$idYouWant=$_REQUEST['myID'];
// dp stuff with this variable to display the correct item...

Related

How to make php dynamic queries

im trying to change query depending on what user has selected. In my case there are displayed two photos with labels pulled from data base. And depending which image is clicked i want that label to be used in query who will display items associated in database with that label. I tried using global variables, but it i didn't work for me. I cant figure out how to insert data into query after user clicked something
There is code fragment where i pull photos with labels from database, and depending on which i select i want "$modeliai_results['Modelis']" (vehicle model label) to be sent and used in another query below
<div class="eile">
<?php
echo '
<a href="standartiniskatalogas.php?page=kategorijos"/>
<img src="data:image/jpeg;base64,'.base64_encode( $modeliai_results['Nuotrauka'] ).'"/>
<div class="description">'.$modeliai_results['Marke'].' '.$modeliai_results['Modelis'].'
</div></a>';
?>
Page from second image which should execute query depending on selection from previous page:
<?php
$kategorijos_sql="SELECT * FROM kategorijos (HERE SHOULD BE ADDED: "WHERE Modelis='MODEL NAME WHICH WAS SELECTED WHEN PRESSED ON IMAGE'";
$kategorijos_query=mysqli_query($dbconnect, $kategorijos_sql);
$kategorijos_results=mysqli_fetch_assoc($kategorijos_query);
do{
echo '
<div class="eile2">
<a href="isore/isore.html">
<img src="data:image/jpeg;base64,'.base64_encode( $kategorijos_results['Nuotrauka'] ).'"/>
<div class="description">'.$kategorijos_results['Kategorija'].'</div>
</a></div>'; /* uzdaro echo*/
}
while ($kategorijos_results=mysqli_fetch_assoc($kategorijos_query))
?>
ps. Im not using any extra tools or libraries.
Selecting object
Display data associated with object
My mistake was that i concentrated on "onclick" event which would set global variable and that global variable would be used in query. Problem was that, php with java script not so big friends and would create not linkable URLs to same content. The solution was to use simple thing "$_GET". Assign $_GET to some php variable, and then use it in "href". That way when you click on any kind of object with "a href="'.$variable.'"> bla bla /a>" you get redirected to page with added tail to URL from which you can access to use in other queries
<?php
$model= $_GET['model']; //variable from URL tail
$category= $_GET['category'];
$category_sql="SELECT * FROM service WHERE Model='$model' AND Category='$category'"; //my SQL where i use variables selected from URL
$category_query=mysqli_query($dbconnect, $category_sql);
$category_results=mysqli_fetch_assoc($category_query);
do{
echo ' //print html together with php code
<tr class="data">
<td>
<a href="catalogue.php? page=service&model='.$model.'&category='.$category.'&service='.$category_results['Name'].'"> //here i make href which when clicked will pass values named with $ sign
<img src="data:image/jpeg;base64,'.base64_encode( $category_results['Image'] ).'"/>//Here i take image from data base
<br><button>'.$category_results['Name'].'</button></a>
</td>
<td> <p>'.$category_results['Description'].'</p> //I take values from data base
</td>
<td>'.$category_results['Price'].' €</td>
</tr>';
}
while ($category_results=mysqli_fetch_assoc($category_query)) //loop which cycles through all data base items
?>

PHP How to get userID (variable) in pop up modal

A number of employees and their info gets shown on a page. The info of employees gets retrieved via a DB and then a foreach() loop is used to display all employees who fit the search criteria, an example can be seen on image below
Now when the user clicks the button, a simple Bootstrap pop up modal gets triggered, with some basic form fields. As can be seen from example image below
My Problem
I need to get the $userID when a button is clicked to work with / process data in modal.
Below is an extract of relevant code:
$teacherClass = new TeacherSearch();
$teachers = $teacherClass->showAllTeachers();
if (is_array($teachers)) {
foreach ($teachers as $teacher) {
$src = $teacher['userID'];
<div class="teacher-info">
<p class="teacherLabel">
NAME:
<?php
echo $teacher['name'];
?>
</p>
<p class="teacherLabel">
HEADLINE:
<?php
echo $teacher['headline'];
?>
<p class="teacherLabel">
LOCATION:
<?php
echo $teacher['location']
?>
</p>
<!--BUTTON GOES HERE-->
}//foreach
What I've tried
Ive tried using an <a> element binding a parameter with userID to it, like so:
Hire <?php echo $teacher['name'] ?>
As is to be expected the following triggered a new page reload inside the modal.
I then tried using a # sign for ahref attribute and then biding parameter $userID to it like so:
The above lead to an undefined index error as can be seen in the picture above.
Conclusion
I hope this question makes sense, I'm pretty much out of ideas, no idea how to further approach this problem.
You add the user-id in the anchor tag.To get the contents of the attribute data-id you have to use
<a id="myid" data-id="123">link</a>
<script>
$('#myid').data("id");
</script>
use javascript function to get userID and show modal:
<a onclick="myfunction('<?php echo $userID; ?>')>Hire <?php echo $teacher['name'] ?></a>
javascript function:
var userID; // global
function myfunction(id){
userID = id;
$("#myModal").modal("show");
//do somethings with user id here
}

How to echo value from different page?

I have a table like this :
When I click update link, I want to echo the "nim" column (first column) to this page inside the nim input text.
Here is my controller
public function fupdate() {
$this->load->view('update_form_mhs');
}
public function update() {
}
Here is my view:
<tr>
<td>NIM</td>
<td><input type="text" placeholder="enter nim" name="nim"></td>
</tr>
How do I echo that?
You can send data from the listing page through GET method like:
9004
And in the form page,
get nim through $_GET['nim']
So, the corrected code:
<td><input type="text" placeholder="enter nim" name="nim" value="<?php echo $_GET['nim']?>"/></td>
In the update method you need to do the following:
Grab the ID from the URL, which I assume is the number at the end of the update link (4441 in this case).
Fetch the row from the database which corresponds to this ID.
Echo out the details from that single row into the HTML code where you want it.
Now, since you haven't posted any code which relates to the actual fetching of the data, I'm afraid I cannot help you further with that. However, this step is pretty basic and should be well documented in the CI manual.

PHP: How to populate a page dynamically once the user clicks on the URL

With help from #teresko I've managed to create dynamic pages (& their urls) for my site using the loop below. My problem is how do i get the newly created page at ahref to combine data I have in database with the template (which I already have ready), so that when the user clicks on it, she/he goes to the page populated with the data. Am i supposed to use a javascript click function (would rather not). How would i do it with php and html?
Here is the loop generating the URLs:
<?php foreach ($reciperow as $recipe) { ?>
<h2><?php echo $recipe['rectitle'];?></h2>
<p class="subhead"><?php echo $recipe['recsummary']; ?></p>
<?php } ?>
Would really appreciate a solution that stays clear of routing, since my site's a basic project and I plan to get to MVC and PHP routing in the next projects. Thanks.
If you meant How, when user clicks generated link, show him page with data from database accordingly to "id" he/she selected, then do the following:
<?php
$id = intval($_REQUEST['id']);
if ($id) { // user get here by clicking on link with id
$data = ... // fetch data from database
?>
<sometag>Some data from database:<?php echo $data['somecollumn']; ?></sometag>
...
<?php
} else {
// user just opened first page
// generate links list as usual
...
foreach ($reciperow as $recipe) {
?>
<h2><?php echo $recipe['rectitle'];?></h2>
<p class="subhead"><?php echo $recipe['recsummary']; ?></p>
<?php
}
...
}
?>
Edit:
Is this how it is normally done for simple sites?
Depends.
If you has only one entity in datadabe, then there will be no arguments clash, id is identifier only for recipies, but if you intent to show also details for, f.e. ingredients, furniture and/or more, then you must add specificators.
Like, links will look like
<a href="?show=recipie&id=<?php echo $recipe['uniqno'];?>">
<a href="?show=ingredient&id=<?php echo $ingredients['id'];?>">
... and then data must be fetched and displayed correspondingly:
$id = ...;
if ($id)
switch ($_REQUEST['show']) {
case 'recipie':
// show recipie data
break;
case 'ingredient':
// show ingredient data
break;
case ...
default:
// show start page
}
But with addition of another entities yours .php file will grow. Another solution will be to add separate scripts for handling each entity:
// generate links list as usual
...
foreach ($reciperow as $recipe) { // look at `recipie.php` portion of link's href
?>
<h2><?php echo $recipe['rectitle'];?></h2>
<p class="subhead"><?php echo $recipe['recsummary']; ?></p>
<?php
}
And add recipie.php file in the same folder as base script with following contents:
<?php
$id = intval($_REQUEST['id']);
if ($id) { // user get here by clicking on link with id
$data = ... // fetch data from database
?>
<sometag>Some data from database:<?php echo $data['somecollumn']; ?></sometag>
...
<?php
} else {
?>
<h1 class="error">No recipie ID specified</h1>
<?php
}
<?
Further exploring will bring you to concepts of MVC and routing via human-friendly-links format, when links looks like /home, /recipie/12 and/or /recipie/?id=12 or even /recipie/12-cream-pie. But that's story for another time...

When a user clicks on a link, how can I send a php variable to that new page

What I am trying to do is:
a user as a list of venues to click on from a table (loaded From sql)
When they click on a venue the user is sent to venuePage.php
What I need is the primary key of the venue the have clicked on.
I have tried using $session variables but so far no luck. here is my code:
<td id="startingAt"> <a href="clubPage.php"
onClick="<?php $_SESSION['currentVenueId'] = $venueID;?>">
<p> <?php echo $venue; ?> </p> </a> </td>
Currently I am just echoing out $_SESSION['currentVenueId'] and the result does not update.
Any help would be really appreciated! Thank you in advance!
You could try this:
<td id="startingAt"> <a href="clubPage.php">
=>
<td id="startingAt"> <a href="clubPage.php?venueID=<?php $_SESSION['currentVenueId'] = $venueID;?>">
And in the clubPage.php you could get the value with $venueId = $_GET['venueId']
Using $_GET you can simply put your variable within the URL:
...
Then pull it using:
if ( isset($_GET['currentVenueId']) )
$venueId = $_GET['currentVenueId'];
If you have more than one variable, you can combine them with an ampersand.

Categories