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
?>
Related
I want to be able to assign a post to a term when adding or editing a term. I thought it might be possible to do this with ACF save_post. Can you help me for the php code? Thanks!
From what I understand Acf Save post saves your $_POST data. Not sure if you mean post as in blog posts or if you're referring to $_POST data from a form.
As an alternative, If you're using MYSQLI in your website you can assign your $_POST data to an assoc array when you submit. So alongside your other database entries, you can write code to assign these $_POST variables to an assoc. array of terms you've defined.
First, I'd suggest created a database of your terms that you can edit. Write HTML code for your posts that asks for both Post_title and relevant term. This way you add values to your posts based on terms and later create code to search your database for posts that use those terms (similar to how tags work).
So to remember, you need to create a database that has a value for post_title and terms and a seperate database for your posts alone. The second database essentially acts as an assoc. array between posts and terms.
<div>
<form action='' method="post">
<div>
<?php
if (isset($_POST['submit'])) {
// If you have additional text input/images or anything else, create a variable based on the 'name' you assigned for it in your html field. e.g
$post_text=$_POST['post_text'];
$post_title= $_POST['post_title'];
$post_term = $_POST['terms'];
// A small check to make sure we don't send empty posts without terms. If you don't want this remove this part of the code
if ($post_term == "" || $post_title == "") {
echo "<h4> The field is empty</h4>";
}
// Check ends here. Again if you don't want this check just change the following else statement to an if
else {
// Field_1/Field_2 are the specific names of these fields in your database. 'yourpostsdatabasename' as the name implies is the specific name of your database on MYSQLI. Same applies for the terms database
$addtoposts= "INSERT INTO yourpostsdatabasename (field_1,field_2) VALUES ('$post_title', '$post_text')";
$addtoterms= "INSERT INTO yourtermsdatabase (field_1,field_2) VALUES ('$post_term', '$post_title')";
$postsquery=mysqli_query($connection,$addtoposts);
$termsquery=mysqli_query($connection,$addtoterms);
}
}
?>
<label for="post_title">Post Title</label>
<input type="text" name="post_title">
// You can add more input fields for post text etc, Just make corresponding titles and fields in your database to accomodate this data
<label for="terms">Terms></label>
<input type="text" name="terms">
</div>
<div>
<button type="submit" name="submit"> Add Post </button>
</div>
//To show what terms are associated to what posts, you can write html to display a terms table like so
<div>
<table>
<thead>
<tr>
<td>Post Title</td>
<td>Post Term</td>
</tr>
</thead>
<tbody>
<?php
$term_search="SELECT * FROM yourtermsdatabasename";
$term_query=mysqli($connection,$term_search);
// Remember, the values field_1, field_2 have to be the exact names of the corresponding fields on your terms database. Along with the exact name of your database for the variable $term_search above.
while ($row=mysqli_fetch_assoc($term_query)){
$post_name=$row['field_1'];
$term=$row['field_2'];
echo "<tr>";
echo "<td>{$post_name}</td>";
echo "<td>{$term}</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
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
}
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...
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.
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...