trying to get my head around REST, I am following/copying a tutorial. The "$_get" is blank, I noticed that the URL that is being called is blank here is a copy,
http://localhost/RestClient/index.php?action=get_user&id=
but the href I am clicking looks ok to me.
<a href='http://localhost/RestClient/index.php?action=get_user&id='3' alt=user_'3'>Carbonnel</a>
here is my code I am new to PHP so figuring it all as I go!!!!
<?php
/*** this is the client ***/
if (isset($_GET["action"]) && isset($_GET["id"]) && $_GET["action"] == "get_user") // if the get parameter action is get_user and if the id is set, call the api to get the user information
{
$user_info = file_get_contents('http://localhost/RestServer/api.php?action=get_user&id=' . $_GET ["id"]);
$user_info = json_decode($user_info, true);
// THAT IS VERY QUICK AND DIRTY !!!!!
?>
<table>
<tr>
<td>Name: </td><td> <?php echo $user_info["last_name"] ?></td>
</tr>
<tr>
<td>First Name: </td><td> <?php echo $user_info["first_name"] ?></td>
</tr>
<tr>
<td>Age: </td><td> <?php echo $user_info["age"] ?></td>
</tr>
</table>
<a href="http://localhost/RestClient/index.php?action=get_userlist" >Return to the user list</a>
<?php
}
else // else take the user list
{
$user_list = file_get_contents('http://localhost/RestServer/api.php?action=get_user_list');
$user_list = json_decode($user_list, true);
// THAT IS VERY QUICK AND DIRTY !!!!!
?>
<ul>
<?php foreach ($user_list as $user): ?>
<li>
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id='".$user ['id']."' alt=user_'".$user['id']."'>"; ?><?php echo $user["name"] . "</a>"; ?>
</li>
<?php endforeach; ?>
</ul>
<?php
}
?>
The Link
<a href='http://localhost/RestClient/index.php?action=get_user&id='3' alt=user_'3'>Carbonnel</a>
is incorrect, it must be:
<a href='http://localhost/RestClient/index.php?action=get_user&id=3' alt='user_3'>Carbonnel</a>
Watch the changes in ' signs.
In you example $_GET['id'] must have been always null.
There is definitely something wrong with you <a>.
You are using single quotes for the tag attribute and then for query string parameters too.
Any program having to interpret that will have no idea where the href= actually ends.
One solution would be to use double quotes (") for the attribute and single quotes for the value (if you need those at all).
Change
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id='".$user ['id']."' alt=user_'".$user['id']."'>"; ?>
to
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id=".$user ['id']." alt=user_".$user['id']."'>"; ?>
Related
<?php
$pros_array_id= array_column($_SESSION['cart'], 'paint_id');
$productee = $productn->getData('paint');
foreach($productee as $pro):
if($pro['paint_id'] == $pros_array_id):
?>
<table>
<tr>
<td class="imgTag">
<img class="img-fluid" src="<?php echo $pro['paint_image'] ?? 1; ?>" >
</td>
</tr>
</table>
<?php
endif;
endforeach;
?>
Am trying to display session cart items and its not showing anything. When I print_r($productee) and print_r($pros_array_id) after the foreach statement both display the accurate data, yet nothing displat in the <tr> tag.
The is to display the result
When I implode $pros_array_id like this "$imp = implode(" ",$pros_array_id);" and put the variable in the if-statement, it works fine if only one product is in the session, but the moment I add more than one products in the session, nothing is display again.
Please can someone point to me what I should do?
Thanks
Try it like this:
<table>
<?php
$pros_array_id = array_column($_SESSION['cart'], 'paint_id');
$productee = $productn->getData('paint');
foreach($productee as $pro) {
//Also see and try to disable this check to see if it works then!
if($pro['paint_id'] == $pros_array_id) { ?>
<tr>
<td class="imgTag">
<img class="img-fluid" src="<?php echo $pro['paint_image'] ?? 1; ?>">
</td>
</tr>
<?php
}
}
?>
</table>
Always make sure while debugging this kind of code to eliminate any checks that could result in false, so try disabling the "if clause" as well, to see if it displays something then.
I have a function inside my controller that outputs html elements to a view. This is how it looks like.
function show_res() {
$output = '';
foreach($data->result() as $row) {
if($row->user_agency == $user_agency) {
$output .= '
<tr>
<td>'.$row->user_name.' '.$row->user_lname.'</td>
<td>'.$row->user_type.'</td>
<td>
<button>Edit</button>
<button>Delete</button>
</td>
</tr>
';
}
}
echo $output;
}
My task is that I need to include a php function for when the delete button is clicked, so far this is what I've tried:
if($row->user_agency == $user_agency) {
$output .= '
<tr>
<td>'.$row->user_name.' '.$row->user_lname.'</td>
<td>'.$row->user_type.'</td>
<td>
<button>Edit</button>
<a href="<?php echo base_url().delete_c/delete_user/.$row->user_id; ?>">
<button>Delete</button>
</a>
</td>
</tr>
';
}
and
if($row->user_agency == $user_agency) {
$output .= '
<tr>
<td>'.$row->user_name.' '.$row->user_lname.'</td>
<td>'.$row->user_type.'</td>
<td>
<button>Edit</button>
<a href="'.echo base_url().'delete_c/delete_user/'.$row->user_id'">
<button>Delete</button>
</a>
</td>
</tr>
';
}
The problem with the first one is that the whole
<?php echo base_url().delete_c/delete_user/.$row->user_id; ?>
is added as a string instead of a php script so when I click on the button it would redirect to this URL
https://localhost/test/<?php%20echo%20base_url().delete_c/delete_user/.$row->user_id;%20?>
As for the second one, it causes an internal error resulting to the status code 500
Use this. There is no need to write again PHP start because you already started and you forgot to mention . operator.
<a href='.base_url().'delete_c/delete_user/'.$row->user_id.'> <button>Delete</button>
</a>
The following code should work.
<a class="btn" href='.base_url().'delete_c/delete_user/'.$row->user_id.'>
Delete
</a>
500 error code may be cause of you forgot to put . in string concatenation or may be undefined variable used in function.
I have selecting data from MySQL table and returned data looks like:
Name Rank Vessel Type Preview
John 1 ab View
Lisa 1 cd View
Carl 2 bd View
As you see above View should be link to user's profiles depending on their Id.
For now I'm using table as you see below and this is line for View link:
<?php echo "<td> View</td>"; ?>
I don't know why, but It redirecting users like: www.etc.com/preview.php?Id= without provided Id.
If I'm using something like:
<?php echo "<td> View " . $Id . "</td>"; ?>
It returning View Id for example View 1, that's mean $Id variable is fine. Have you ideas what's wrong?
Also I want to ask you If it is correct way for redirecting? Or there is any other / better way to do that?
This is PHP:
<?php
if(isset($UserID)) {
$users = $con->prepare("
SELECT DISTINCT Id,
CONCAT(FirstName, ' ', LastName) AS FullName,
RankApplied,
VesselsType
FROM tbl
");
$users->execute();
$users->bind_result($Id, $FName, $RankApplied, $VesselsType);
} else {
echo "There is no User ID detected, try to refresh browser.";
}
?>
<table>
<tr>
<th>Name</th>
<th>Rank</th>
<th>Vessel Type</th>
<th>Preview</th>
</tr>
<?php
while ($users->fetch()) {
?>
<tr>
<td><?php echo $FName; ?></td>
<td><?php echo $RankApplied; ?></td>
<td><?php echo $VesselsType; ?></td>
<?php echo "<td> View</td>"; ?>
</tr>
<?php
}
?>
</table>
You are closing your href quote before including the id, change the line to the following to get it to work:
<?php echo "<td> <a href='preview.php?Id=" . $Id . "'>View</a></td>"; ?>
Replace
<?php echo "<td> View</td>"; ?>
with
<?php echo "<td> View</td>"; ?>
You are having a ' after ?Id= which was creating the problem..
Instead of concatenating the $ID you could directly use
<?php echo "<td> <a href='preview.php?Id=$Id'>View</a></td>"; ?>
As in the docs
Double quote strings will display a host of escaped characters
(including some regexes), and variables in the strings will be
evaluated. An important point here is that you can use curly braces to
isolate the name of the variable you want evaluated. For example let's
say you have the variable $type and you what to echo "The $types are"
That will look for the variable $types. To get around this use echo
"The {$type}s are" You can put the left brace before or after the
dollar sign. Take a look at string parsing to see how to use array
variables and such
Please see this answer
Solving your problem:
<?php echo "<td> <a href='preview.php?Id=".$Id . "'>View</a></td>"; ?>
Alternatively you may define before using it:
<?php $link = "preview.php?Id=$Id"; ?>
<?php echo "<a href='$link'>View</a>"; ?>
Also you may try PHP in HTML
<a href='<?php echo "preview.php?Id=$Id"?>'>View</a>
I had to display a table using php and html, that select data from sql server. Their is a cell called echo photo that sometimes is empty, and sometimes is loaded with an image. How to change my code to test:
if (it is empty){
echo "No echo files";
}
else{
<a href='download_PopUp.php?data=<?php echo $dataFile2; ?>'>Click to download</a>
}
My php and html codes are:
<?php
...
while($rows=mysqli_fetch_array($result)){
$data =$rows['echo_photo'];
$dataFile = str_replace('/', '\\', $data);
...
?>
here in this td of html table I want to put if else conditions
<td align="center"><a href='download_PopUp.php?data=<?php echo $dataFile2; ?>'>Click to download</a></td>
<?php if (!empty($data)) : ?>
<td align="center"><a href='download_PopUp.php?data=<?php echo $dataFile2; ?>'>Click to download</a></td>
<?php else: ?>
<td align="center">No Data Available</td>
<?php endif; ?>
Why dont you change the query.
Select * FROM table WHERE echo_photo NOT LIKE ''
Than you will only get the values that exists
Otherwise you can do it with a if statement like:
if(!empty($data)){
// echo html code
}else{
echo "No Data Found!";
I have represented data in tabular form with no problem. But when i click the delete button i want the $id i.e. row[0] of my database to pass to another page delete_user_post.php where i can capture this value of $id using $_REQUEST. When i echo $id it shows its associated id. But i am not able to pass that value in next page.what have i done wrong??
`
<tbody>
<?php while($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row[0] ?></td>
<td><?php echo $row[1] ?></td>
<td><?php echo $row[2] ?></td>
<td>
<div class="control-group">
<label class="control-label"></label>
<div class="controls">
<?php $id = $row[0];
echo $id;
?>
Delete
</td>
</div>
</tr>
<?php } ?>
</tbody>
My user_delete_post.php looks like this:
<?php
include ('include/DB_Functions.php');
$id =$_REQUEST['roomid'];
// sending query
mysql_query("DELETE FROM room_tb WHERE roomID=$id") or die(mysql_error());
?>
It looks like you're not actually echoing the $id.
Replace your a tag with this:
Delete
Also please look up mysqli_ or PDO as a replacement for mysql_
Also think long and hard about if you really want unescaped and unverified IDs to be deleted from your database.
Try to put $id in php tags like,
Delete