$row value lost when going to other page using controller - php

Whenever I'm going to the designated page using the controller, $row values are lost.
The code below shows how I get to the new page.
while($row = $result->fetch_assoc()) {
$html .= "<tr>";
$html .= "<td><a href='?article=del&".$row["id"]."'><i class='fas fa-trash-alt'></a></i></td>";
$html .= "</tr>";
}
Normally, in my complete table, all the data is shown, including the ID and the names in the db etc. But when I get to the new page, the $row value is lost. For example the del page would be this:
<?php
function del() {
print $row;
}
I would get this error:
Notice: Undefined variable: row in C:\wamp64\www\SMS2\article\del.php on line 3
Instead, the url DOES show the $row["id"] for example:
http://localhost/SMS2/index.php?article=del&15
In this case I clicked on the row which had the 'id' value of 15.
So basically, the $row value does get loaded in the url, which is 15, but is lost in the actual page when I try to print it.

While I can't see the rest of your code, your URL is missing the definition of what 15 is. You need to have a variable in the URL. For example:
http://localhost/SMS2/index.php?article=del&id=15
Instead of
http://localhost/SMS2/index.php?article=del&15
Then in your code you would use something like $_GET['id'] to pull the value of 15 out of the URL.

Change
<a href='?article=del&".$row["id"]."'>
into:
<a href='?article=del&id=".$row["id"]."'>
and on the other page:
<?php
$id = $_GET['id'];
print $id;
?>

Related

Deleting a database row on front end of Wordpress site

I have created a front end page on my Wordpress site where I can submit data through a form, and it will display all the data in a table on the same page. I would like to have a delete option for each row but I am not sure which way to go.
The code I am using to display the database information works fine.
/* Displays Table and Data*/
foreach($cogs as $cogs){
echo "<tr>";
echo "<td>".$cogs->id."</td>";
echo "<td>".$cogs->date."</td>";
echo "<td>".$cogs->source."</td>";
echo "<td>".$cogs->items_purchased."</td>";
echo "<td>".$cogs->receipt_total."</td>";
echo "<td>" "</td>";
echo "</tr>";
}
In the line after "receipt_total," what code should I write? Should I include an anchor tag and link the delete button to another page? I have tried an HTML link, and a HTML button. My problem is making the button or link delete the row from the database. Someone suggested this code, but I am not sure I am using it right. My gut instinct is to place this code in my functions.php file, but I am not sure.
<?php
if(isset($_GET['id']) &&!empty($_GET['id'])){
$id = $_GET['id'];
$table = 'cogs';
$wpdb->delete( $table, array( 'id' => $id ) );
wp_redirect('URL');
}
?>

PHP - how to assign value in a whileloop and accessing them on other page

For example this whileloop.
page1.php
<?php
while($row = mysqli_fetch_array($query))
{
?>
<h2><?php echo $row['sample'];?></h2>
Page2 Link
<?php
$_SESSION['samp'] = $row['sample'];
}
?>
FOR EXAMPLE
The whileloops displays 3 array h2 tag each with a link tag
sample1 sample2 sample3
page2 Link page2 Link page2 Link
Now in page2.php I want to display the value of the clicked link
<h2><?php echo $_SESSION['samp']; ?></h2>
My problem is that if I click any of the link tag only the value of the first array is displayed in page2.php
First you are discarding any old values stored in session:
$_SESSION['samp'] = $row['sample']; /* overrites */
Second, if you can manage to use query string, then you may find it much easier:
Page2 Link
Third, now just use the $_GET to get the value of the clicked link:
<h2>Sample: <?=$_GET['sample']?></h2>
The problem is that you're assigning to $_SESSION['samp'] in a while loop, thus overwriting the value each time the loop runs. To correct this, you should append to the $_SESSION['samp'] array instead:
$_SESSION['samp'][] = $row['sample'];
Then you can loop over the array on the second page:
foreach ($_SESSION['samp'] as $value) {
echo "<h2>$value</h2><br />";
}
I think you dont need to store the values in session, you can directly send it along with page name and get that value on another page like below
page1.php code like below
<?php
while($row = mysqli_fetch_array($query))
{
?>
<h2><?php echo $row['sample'];?></h2>
Page2 Link
<?php
}
?>
page2.php code like below
<?php echo $sample = $_GET['sample'];?>

Pass the id of href into the href link

So here is my problem.I want the id of the href which is taken dynamically to be printed in the url.
My code of category.php:
CATEGORY.PHP
while($row = mysql_fetch_assoc($result))
{
echo '<a id="' . $row['topic_id'] . '" href="category.php?id=' .
$_SESSION['id'] . '&check1=//what should i put here?">;
}
all i want is for check1 to have the id of each link seperately so if i have 3 links(depends on the database) the 2nd link will have id=2 and i want to print id=2 the next time category.php is loaded.
BUT
Notice that all id are first printed and then someone will choose one of these links.
You're already doing it in other places.
It's called concatenation - All that PHP does is output dynamically generated HTML. This means that within the quotes of your echo construct you can concatenate anywhere.
All you have to do is add it to the URL as you did with ID:
while ($row = mysql_fetch_assoc($result)) {
echo '<a id="'. $row['topic_id'] .'" href="category.php?id='. $_SESSION['id'] .'&check1='. $row['topic_id'] .'"></a>'
}
your $row['topic_id'] should be different with every iteration. the $row variable gets set to a new
This will output the same $row['topic_id'] as the id attribute of the link but it will also append it as a $_GET parameter in your category.php.
If you do not know what gets passed on in the $_GET array you could always do a print_r($_GET) anywhere in the script since $_GET always gets set (even without query params, it'll just be empty).
It could be that it is unclear to me what you're asking, it could also be that you want the actual primary key id field in which case you'll just have to swap out the last bit:
&check1=$row['topic_id']
with whatever your ID field is named, probably something along the lines of:
&check1=$row['id']
where id would be the actual id of the row in the database table.

Pass data from php mysql to pop up on same page

I have a link in a php while loop
echo "<a href = '#$product_id' onclick = 'pop_up()' id = 'linker'>See more</a>";
The pop up requires the product id to search the database but hash tag is client side. I tried to use javascript window.location.hash but the outcome was not very reliable.
Does anyone know a method preferably server side I could use to retain the active product id while I call the pop up, attain the product id, use it to query the database and output it in the pop up.
I have a session already started and tied to a different condition.
I tried to call the product id directly from the pop up but because of the loop I only get either the first or last in the array.
<?
while ($i < $num) {
$product_id=mysql_result($result,$i,"prod_id");
$title=mysql_result($result,$i,"lTitle");
//main page
echo "<b>" , $title;
echo "<a href = '#$product_id' onclick = 'pop_up()' id = 'linker'>See more</a>";
?>
<!------pop up--------->
<script type="text/javascript">
function pop_up(){
document.getElementById('pop').style.display='block';
}
</script>
<div id="pop">
<p style='color:#6F0A0A; font-size:15px;'><? echo $product_id; ?></p>
</div>
<?
$i++;
}
?>
I'll try answering but to be honest the question is very vague and the code is a bit messy.
First off, you can simply send the product_id as a GET variable to the new popup and read it in PHP. Something like this will work:
echo "<a href = 'http://www.mydomain.com/popup.php?product_id=$product_id' onclick="window.open(this.href, 'popup_win',
'left=100,top=100,width=500,height=500,toolbar=1,resizable=0'); return false;" id = 'linker' >See more</a>";
On your popup.php file (the popup page) you will get the product_id with the PHP $_GET method:
$product_id = $_GET['product_id'];
and then do whatever MySQL query you want, now that you know $product_id.
I hope this helps, if that's not exactly what you meant please add more details so I can revise my answer.
Well, you could first load all this records first and place them into the popup content or, make an ajax request, open the popup, and when the request is done successfully place the values returned into the popup content. Better with JQuery

Display personal messages list

I have a personal message system in my website done simply with php/sql. Actually I am facing the trouble to display them using jquery. The db has as fields: message_id, message_from, message_to, message_topic, message_subject and message_status. The way I am showing the message_topic is repeating eight times the following:
echo '<table><tr><td>';
retrieve_msg_topic($result);
echo '</td></tr>'; //of course I won't make 8 tables!!!
the function called is:
function retrieve_msg_topic($result)
{
if($row = mysql_fetch_assoc($result))
{
echo $row['usernombre'];
$message_topic = stripslashes($row['message_topic']);
echo '<div id="msg'.$row['message_id'].'">';
echo $message_topic;
echo '</div>';
//this will return: <div id="msgN">message topic (title, commonly subject)</div>
}
} //end function retrieve msg topic
So far I have a list on a table with the last eight messages sent to the user. The following row is reserved for pagination (next/prior page) and, after that, another row showing the message I select from the list presented, like we see in Outlook. Here is my headache. My approach is to call another function (8 times) and have all of them hidden until I click on one of the messages, like this:
echo '<tr><td>';
retrieve_msg_content($result);
retrieve_msg_content($result); //repeat 8 times
echo '</td></tr></table>';
the function this time would be something like this:
function retrieve_msg_content($result)
{
if($row = mysql_fetch_assoc($result))
{
echo '<script type="text/javascript">
$(document).ready(function(){
$("#msg'.$row['message_id'].'").click(function(){
    $(".msgs").hide(1000);
$("#'.$row['message_id'].'").show(1000);
});
});
</script>';
echo '<div class="msgs" id="'.$row['message_id'].'" style="display: none">'
.$row['message_subject'].
'</div>';
}
/* This function returns:
// <script type="text/javascript">
// $(document).ready(function(){
// $("#msgN").click(function(){
// $(".msgs").hide(1000);
// $("#N").show(1000);
// });
// });
// </script>
// <div class="msgs" id="N" style="display: none">Message subject (body of message)</div>
*/
} //end function retrieve msg content/subject
I could simply explain that the problem is that it doesn't work and it is because I do if($row = mysql_fetch_assoc($result)) twice, so for the second time it doesn't have any more values!
The other approach I had was to call both the message_topic and message_subject in the same function but I end up with a sort of accordion which is not what I want.
I hope I was clear enough.
The easiest way to fix your troubles would be to copy the results of the MySQL query into an array
while($row = mysql_fetch_assoc($result)) {
$yourArray[] = $row;
}
And then use that to build your tables.
edit: What I meant was more along the lines of this:
while($row = mysql_fetch_assoc($result)) {
$yourArray[] = $row;
}
echo '<table>';
foreach($yourArray as $i) {
retrieve_msg_topic($i);
}
echo '<tr><td>';
foreach($yourArray as $i) {
retrieve_msg_content($i);
}
echo '</tr></td></table>';
And then removing everything to do with the SQL query from those functions, like this:
function retrieve_msg_topic($result) {
echo '<tr></td>'$result['usernombre'];
echo '<div id="msg'.$result['message_id'].'">';
echo stripslashes($result['message_topic']);
echo '</div><td></tr>';
}
Right now you're doing some weird key mojo with ret[0] being the topic and $ret[1] being the message, which isn't a good practise. Also, I don't see the declaration of $i anywhere in that code.
The error suggests that the result is empty or the query is malformed. I can't be sure from the code I've seen.
A few other notes: it seems weird that you're using stripslashes() on data that's directly from the DB. Are you sure you're not escaping stuff twice when inserting content into the DB?
Always use loops instead of writing something out x times (like the 8 times you said in your question). Think of a situation where you have to change something about the function call (the name, the parameters, whatever). With loops you have to edit 1 place. Without, you need to edit 8 different places.
BTW, another solution to this problem would be using AJAX to load content into the last cell. If you're curious, I could show you how.
more edits:
For AJAX, build your message list as usual and leave the target td empty. Then, add a jQuery AJAX call:
$('MSG_LIST_ELEMENT').click(function() {
var msgId = $(this).attr('id').replace('msg','');
$.get(AJAX_URL+'?msgID='+msgId,function(data) {
$('TARGET_TD').html(data);
})
});
Replace the capitalized variables with the ones you need. As for the PHP, just echo out the contents of the message with the ID $_GET['msgID'].
However, make sure you authenticate the user before echoing out any messages, so that someone else can't read someone's messages by switching the id number. Not sure how authentication works on your site, but this can be done by using session variables.

Categories