Trouble getting a value from <a> link - PHP - php

My problem is that I need to save a value from the link as a variable which will then do something. Below is my main code for this problem.
<table>
<? foreach ($csv as $row) : ?>
<tr>
<td><? echo $row[0]; ?></td>
</tr>
<? endforeach; ?>
</table>
Basically, it prints the first column from my array $csv. However I want to save the '$row[0]' for each link - depending on which one is clicked.
This happens here:
<?php
if (isset($_GET['GoToProfile'])) {
}
?>
This works. E.g. when something is clicked it prints something. But I cannot find a way to save the values from each link. Depending on which one is clicked. I have tried many different methods online, but none seem to work.
I have even tried:
<? echo $row[0]; ?>
Any help would be greatly appreciated.
Thanks!

Use an ampersand (&) instead of a question mark
<? echo $row[0]; ?>
The ? indicates the beginning of the query string, which is the data sent on a GET request. In most cases it is a collection of name/value pairs, separated with & s.
A simple example of a GET request
http://example.com?first=1&second=fifty
You would get the value of the parameters in PHP with $_GET
$first = $_GET['first'];
$second = $_GET['second'];
To see what the server is receiving, you can use var_dump
var_dump($_GET)

Related

How to pass data in url format

I'm currently making a table where the email and phone number of clients are clickable. When you click it, it will redirect to another page and execute a filter search on that page.
I wanted to send the data of client email or phone number via the URL, but it seems you can only type in the exact filter search. I wanted when the user clicks a hyperlink it will send the clicked data via URL and execute the filter search.
<?php
foreach($checked_columns as $key=>$column){
if ($column){
if ($key == 'client_email'){?>
<td>
<?= $sale['client_email']; ?>
</td>
<?php } elseif ($key == 'client_phone_number') { ?>
<td>
<?= $sale['client_phone_number']; ?>
</td>
<?php } else {?>
<td><?= $sale[$key]; ?></td>
<?php }
}
}
?>
How can I change the last part of the URL so that it can search it via variable.
Your code can be refined/refactored to utilize the variable data.
foreach ($checked_columns as $key => $column) {
if ($column) {
echo "<td>";
if (in_array($key, ['client_email', 'client_phone_number'])) {
echo "{$sale[$key]}";
} else {
echo $sale[$key];
}
echo "</td>";
}
}
The $key value doesn't look like it needs to be encoded for insertion into the url, but the email seems like a good candidate.
By "staying in" php, I think you will find the snippet easier to read.
If I understand you correctly, currently you are outputting static text:
<a href="admin/sales/browse/confirmed-sale?fc=client_phone_number&fv=60192597698" class="style1">
And you want to output variables instead. Consider that you already have an example in your code of how to output the value from a variable:
<?= $sale['client_phone_number']; ?>
Simply apply that same exact operation for your URL. The only addition would be to ensure that you URL-encode the value, in case it has non-URL-friendly characters. Something like this:
<a href="admin/sales/browse/confirmed-sale?fc=client_phone_number&fv=<?= urlencode($sale['client_phone_number']); ?>" class="style1">
There are a variety of ways you can reorganize your code, perhaps putting the HTML into strings and using echo or perhaps applying a PHP templating engine to separate content from markup. How you organize it is really up to personal preference. But at its simplest, if you want to output a value then just output the value like you already do.

Undefined Index $_GET

1st Page that links to the 2nd
<td>More Detail</td>
2nd Page
$toyId=$_GET["toyInfo"];
$question = "SELECT * FROM toy, toyscountry, toysmedia, alltoyscategory WHERE toy_id= '$toyId' AND toy.toy_country = toyscountry.tCou_id
AND toy.toy_id = toysmedia.tMedia_toyId
AND toy.toy_id=alltoyscategory.allToysCat_toyId";
$reply = mysqli_query($dbConnection, $question);
echo $question;
ERROR FOUND AFTER ECHO
Notice: Undefined index: toyInfo in C:\Program Files (x86)\EasyPHP-12.0\www\CA1\moreToysDetail.php on line 5
SELECT * FROM toy, toyscountry, toysmedia, alltoyscategory WHERE toy_id= AND toy.toy_country = toyscountry.tCou_id AND toy.toy_id = toysmedia.tMedia_toyId AND toy.toy_id=alltoyscategory.allToysCat_toyId
They couldnt detect toyInfo :( Any kind souls that can help? Greatly appreaciated, I need to hand in my assignment in a few hours time >.<
I spot 3 things that might be wrong with your code:
first:
<td>More Detail</td>
should look like
<td>More Detail</td>
This assumes your php is configured to accept the shorthand <? in stead of the default <?php
second:
is your toy id in the databse not stored as an integer? It pobably should, as it is best practice for an id. This means you don't need the single quotes in you SQL statement:
... WHERE toy_id= $toyId AND ...
third:
Your final line of code does not make much sense. You are echoing the SQL statement. I believe you'ld want to echo the reply. Have a look here http://www.php.net/manual/en/class.mysqli-result.php to find out how to do this, as the reply currently holds an object that you can not just use echo on to display the data.
Your code has a unexpected "=" so that must have been the problem, you also needed to echo $currentToy["toy_id"];
<td>More Detail</td>

removing item from multidimensional array using attribute

I am trying to display an array of data produced from an sql query. this works fine. However I now wish not to display 1 item where the id attribute = a value.
code:
<?php foreach($sheet_list as $key => $sheet) : ?>
<?php if($sheet['id'] == $selected1){unset ($sheet_list[$key]);}?>
<tr>
<td><?=$sheet['title'];?></td>
<td><?php echo date('d F Y', strtotime($sheet['startDate'])); ?></td>
<td><?php echo date('d F Y', strtotime($sheet['endDate'])); ?></td>
<td><input type="radio" name="selected2" id="selected2" value="<?=$sheet['id'];?>" <?php
if ($key == 0) echo ' checked ';
?>/></td>
</tr>
<?php endforeach; ?>
so this is producing a table which is fine, however it is in the foreach loop where i want NOT to display 1 array item and all its attributes where the "id" field is = to the variable $selected1
I have seen lots of questions posted here, however many for 1 or two dimensional arrays.
Please help.
You're close, you should be using continue; instead of unset():
<?php if($sheet['id'] == $selected1){ continue; } ?>
continuePHP Docs makes the loop skip to the next iteration, effectively skipping the current iteration, which is what you want to do.
The benefit here is that you never modify for you $sheet_list array, unlike with unset() where you'll remove that item from the array.
If you really want to unset $sheet_list[$selected1] why don't you just do it before you enter the loop?
If you just want to skip output for this item, then use continue as suggested by nickb.

show hide div based on mysql table row value

hi guys im trying to show and hide div according to mysql value but i couldnt do it can you help me what im doing wrong
here is my code thanks a lot for your ideas
var Value = <?php echo json_encode($valuek) ?>;
if (Value==1){
$('#show_hide').show();
}
else{
$('#show_hide').hide();
}
<?php
$valuek = $session->userinfo['vcc'];
?>
<div id="show_hide">
some code
</div>
<?php echo json_encode($valuek) ?>
will return a json string, instead try just using "echo"
<?php echo $valuek ?>
If all you are going for is a boolean value then there is simply no need for JSON.
Echo the value directly into the JavaScript. Remember to ensure you are passing a valid boolean value.
PHP code -
<?php
$showDiv = ($dbValue == 1? 'true' : 'false');
?>
JavaScript + PHP injection -
<script>
var value = '<?php echo $showDiv; ?>';
<script>
Don't forget to wrap the PHP injected value with quotes.
$valuek = $session->userinfo['vcc'];
I'm not sure if you have the code in this order in your php file, or just showed pieces of code in this order, but Should go BEFORE your js code. It has no value when js code is run.
To see what $valuek is, just echo it on top of the screen
<?php echo "<h1>$valuek</h1>" ?>.
Or just look at the source - at your js function, to see what is printed after 'var Value ='
That's the main thing really - to make sure that you getting what you expect from session.
And as been said, you don't need jason_encode, but you do need a semi-colon after echo command.
Also, I hope your jquery code is within $(document).ready function, not as is.

Variable inside a php echo function

I have a php function which displays a rating bar with the arguments. I have a variable called itemID inside my php page which holds the unique item number. I need to send this value to my function and also echo command must stay. Is there a way to achieve this?
Here is the code, which does not work. When I try it on the server, it does not show the id of item, it prints the variable name as it is.
<?php echo rating_bar('$as',5) ?>
What I get at html file:
<div id="unit_long$as">
instead of the item id in place of $as.
Single Quotes do not support variable replace,
$as = "test";
echo '$as'; //$as in your end result
echo "$as"; // test in your end result
echo $as; // test in your end result
//For proper use
echo " ".$as." "; // test in your end result
Update for newer PHP versions you should now use Template Syntax
echo "{$as}"
If I get what you are saying, this is what you are asking.
<?php echo rating_bar($itemID,5); ?>
With the limited code you are providing, thats what looks like you are asking.

Categories