Send static data from one php to another - php

I have a php file results.php with a grid of different items (each item is a row from the result of a certain query). When I click an item it takes me to another php file details.php where I want to show more details in the new page about the specific item I clicked.
function showAsItem($row) {
$defaultValue = $row[1];
$itemClassname="item";
$titleClassname="title";
$imageClassname="image";
$infoClassname="infolist";
$link="details.php";
$target="_top";
$img="https://barcelonando.com/es/wp-content/uploads/2012/12/barceloneta.jpg";
$alt="Imagen de $row";
echo '<div class="'.$itemClassname.'">';
echo '<div class="' .$titleClassname. '">';
echo '' .ucfirst($row[0]). '';
echo '</div>';
echo '<div class="' .$imageClassname. '">';
echo '<a href="' .$link. '" target="' .$target. '">';
echo '<img src="' .$img. '" alt="' .$alt. '"/>';
echo '</a>';
echo '</div>';
echo '<div class="' .$infoClassname. '">';
echo '<ul>';
echo '<li> Población: ' .$row[1]. '</li>';
echo '<li> Esperanza de vida: ' .$defaultValue. '</li>';
echo '<li> Inmigrantes: ' .$defaultValue. '</li>';
echo '<li> Transportes: ' .$defaultValue. '</li>';
echo '<li> Accidentes: ' .$defaultValue. '</li>';
echo '</ul>';
echo '</div>';
echo '</div>';
}
I only know how to send info from one webpage to another when it is an input type, but in this case I want to send the item static info (I just need to send the name of the item I clicked). Any idea about how to do it?

As #pavel pointed out, using a URL parameter should work for you. Every item must have a unique number or name associated with them.
Do not use their array index or similar, because that will change in the future.
Your example code has $row use numeric indexes - whichever one of them is unique you can use as a URL parameter. If you don't have one, you have to create one. Preferably one that will not change when the data is updated. That is where databases will use numeric IDs, but you can use a string as well, as long as you can identify the item from the details.php.
Example link: $link="details.php?id=" . $row[2];. Replace 2 with whatever is a unique number / name for every row.
Instead of only supplying the $row in your function, you can supply the row's ID as a separate parameter if that is what you need. Depends on how you organized this outside of your example code.
In this case you change the function as well:
function showAsItem($row, $rowId) {
// ...
$link="details.php?id=" . $rowId;
// ...
}
In details.php you can access this variable with $_GET: https://www.php.net/manual/en/reserved.variables.get.php
$id = $_GET['id'];

Related

If the image field is empty how do I get it to show nothing

I have the following image field code but if there's nothing there then the alt stage and src= parts still show, how can I get rid of them if the field is empty?
echo '<div class="tab-inline">';
echo '<img src="' . esc_url( $tension_knit ) . '" alt="tension knit icon" />';
echo '</div>';
you'll want an if statement for that (as mentioned in comments) and if it were me I'd do something like:
echo '<div class="tab-inline">';
if(isset($tension_knit) && $tension_knit != "") {
echo '<img src="' . esc_url( $tension_knit ) . '" alt="tension knit icon" />';
}
echo '</div>';
That's going to produce an empty div for you if the $tension_knit isn't there, you'll have to decide if that's what you want or not. Your question asked specifically about getting rid of the image/alt. If you didn't want the div at all in case of empty $tension_knit, then you could just move the "if" up a line and the closing tag down a line. Good luck!

How to reinsert a value on onclick inside a page?

I would know how to take the value on onclik for inject inside a sql request.
This code below create a link. When I click on the link a specific table appear
$value can two elements
$value['name']
$value['option_id']
$i = 0;
foreach($option_attributes_name as $value) {
$content .= '<li class="col-md-12"><a class="showTabOption nav-link" data-tableid="' . $i . '" data-toggle="tooltip" data-target="#section_ProductsOptionApp_content" href="'. CLICSHOPPING::link('index.php?&pID='. $_GET['pID'] .'#tab-option'. $i .'&toto=' . $value['option_id']) .'"><i class="fas fa-minus-circle"></i> '. $value['name'] .'</a></li>';
$i++;
}
In my table I have a specific dropdown to display information.
Currently it take all informations by the mysql request, but than I tried to make, it's a dropdown must take only the information on the value onclick ( .'&toto=' . $value['option_id'])
On onclick, how to take the value '&toto=' . $value['option_id'] to reinject inside the request.
Ajax page ?
I don't know if I am clear. If not I will complete this explanation.
Thank you.

"sorry you are not allowed to access this page" when trying to pass variable in URL

I am new to plugin development and currently developing a custom CRUD plugin for a database.
I have created a admin page which shows all the database items in a table, and also i can add to this database. However when i try to pass a variable in the URL to edit a item from the database, i get the "sorry you are not allowed to access this page".
Currently it displays the database items in a table from a query then a for each loop, with a delete button in the row also.
<?php
foreach ($results as $results) {
echo '<tr>';
echo '<td>' .$results->id. '</td>';
echo '<td>' .$results->email. '</td>';
echo '<td><a class="button" href="' .home_url(). '/wp-admin/admin.php?page=member-management%2Fdelete_member.php?id=' .$results->id. '">Remove</a></td>';
echo '</tr>';
}
?>
The problem lies with the href here:
href="' .home_url(). '/wp-admin/admin.php?page=member-management%2Fdelete_member.php?id=' .$results->id. '"
if i replace it with :
href="' .home_url(). '/wp-admin/admin.php?page=member-management%2Fdelete_member.php'"
It works fine and goes to the page i want it to, does wordpress not allow variables passed in URL's?
Fix your variables, replacing the second $results for $result. It works the way it is, but makes your code more readable.
And more important, fix the URL, replacing the second ? with &
foreach ($results as $result) {
echo '<tr>';
echo '<td>' .$result->id. '</td>';
echo '<td>' .$result->email. '</td>';
echo '<td><a class="button" href="' .home_url(). '/wp-admin/admin.php?page=member-management%2Fdelete_member.php&id=' .$result->id. '">Remove</a></td>';
echo '</tr>';
}

How to get the Text Value of a Clicked Link using PHP

I know it's very easy to get the text value of a link using jQuery.
But Isn't it possible to get the text value using only PHP? Please have a look at my code:
echo '<table>'
foreach($array['data']['results']['titles'] as $data) {
$title = $data['title'];
$id= $data['id'];
$url = $data['url'];
echo '<tr>';
echo '<td>' . ''.$title.'' . '</td>';
echo '<td>' . '<a href= ' .$url . ' target="_blank" >IMDb Link</a>' . '</td>';
echo '</tr>';
}
echo '</table>';
This picture shows the output of my code
Suppose the user clicked on the Third movie - Batman: The Animated Series. How do I make my movie.php page look like this -- (image below)
The Link Text (which is $title) should be passed to the movie.php page and also the IMDb Link of the corresponding movie (which is stored in the $url variable)
The Only way I know is using $_SESSION but it won't work in this case as it will only store & pass the last value of the foreach loop
Please help me in this regard. Thanks :)
Considering $id as the row ID for the particular movie from the Database. You can use url encoding over here. Your code must be
echo '<td>' . ''.$title.'' . '</td>';
When clicked your URL will look something like.
movie.php?id=3
On your movie.php file use $id = urldecode($_GET['id']); to get the movie ID and you can fetch the relevant data from the DB again.
Let me know if you have any issues.
Try sending all info with Proper Primary key OR here is runtime solution
echo '<td>' . ''.$title.'' . '</td>';
Don't forget to urlencode the variables
each link should contain the unique filed like $id so your url should be echo '<td>' . ''.$title.'' . '</td>';
You can pass the encoded value of $id so end user cannot guess the value.In movie.php you decode the $id& show information on the basis of $id

pass link text to next page

For my actual web-project, I made a tagcloud. When I click on a tag, it should be passed to the page showRecipesFromTags.php.
<form name="tagform" id="tagform" method="get" action="/showRecipesFromTags.php">
<?php
include('php/getRandomTags.php');
$tagsarr = json_decode($return4634, true);
foreach ($tagsarr['Data']['Tag'] as $key => $tag11) {
echo '<a id="seastags" href="#" onclick="document.tagform.submit()"><font size="' . rand(1,4) .'">' . $tag11['Tag_name'] . '</font> <br/></a> ';
}
?>
</form>
It can already go to this page by clicking on the href, but what I need is to pass the id of the clicked tag. The name of the tag is called Tag_name. The name is shown but I don´t know how the pass the id to the next site. The id is in my JSON array and is called Tag_id.
How can I manage this?
Why do you even need a form? Just use plain links.
Try using this in the foreach loop:
echo '<a id="seastags" href="/showRecipesFromTags.php?tagId=' .$tag11['Tag_id'] . '"><font size="' . rand(1,4) .'">' . $tag11['Tag_name'] . '</font> <br/></a> ';
Your choice are:
use a hidden field
put it in the URL
use a cookie

Categories