Delete entry from page but keep in DB (PHP) - php

Say I had the following:
$stmt = $pdo->prepare("SELECT jobName, jobDesc FROM Jobs");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$jName = $row['jobName'];
$jDesc = $row['jobDesc'];
print '<span class="job">Headline:</span> ' . $jName . '<br />
<span class="job">Description:</span>
<p class="job">' . $jDesc . '</p>';
}
The HTML output would be (for testing purposes)
<span class="job">Headline:</span> test 1
<span class="job>Description:</span>
<p class="job">test 1 Description</p>
<span class="job">Headline:</span> test 2
<span class="job>Description:</span>
<p class="job">test 2 Description</p>
<span class="job">Headline:</span> test 3
<span class="job>Description:</span>
<p class="job">test 3 Description</p>
If I wanted to remove one of these entries from the page, but keep it in the DB, how would I accomplish this with PHP? I'd probably have to put a link or button on each entry to remove it, and I know I can't actually take it off the page permanently with jQuery unless I use something to save the DOM state.
Thank you

I'd suggest adding a flag to the row in the DB along the lines of SHOULD_DISPLAY, and then set the flag for rows you don't want displayed and filter them out in that query (or at display time if for some reason that makes more sense).

There are different ways to accomplish this.
You could create a RESTful controller in PHP to implement basic CRUD operations, and then use jquery to delete/update each html element from your DOM and your DB.
Or you could just add a flag field to your database table, for example called "deleted", that can be true or false and let your php script render the row if it was not previously marked as deleted.

Related

How to make php dynamic queries

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
?>

Refresh the content of a <div> that contains the function call to a oracle database

I have a question with my programming assignment,
I have a fabrication process program that populates the data overtime that means the database value changes every seconds.
I have a function that takes the sum over the data changes which means the sum always changing over time.
Function is like this, (I am using Oracle 11g)
CREATE OR REPLACE FUNCTION WELTESADMIN.GET_BLDG_SURF(PROJNAME IN VARCHAR)
RETURN NUMBER IS TOTAL_BLDG_SURF NUMBER(15,3);
BEGIN
SELECT SUM(4 * MD.TOTAL_QTY * MD.SURFACE) INTO TOTAL_BLDG_SURF
FROM MASTER_DRAWING MD WHERE MD.DWG_STATUS = 'ACTIVE'
AND MD.PROJECT_NAME = PROJNAME;
RETURN(TOTAL_BLDG_SURF);
END;
and I am calling tat function in my PHP code like this,
$surfaceSumSql = "SELECT GET_BLDG_SURF(:projName) SURFACETOTAL FROM DUAL";
$surfaceSumParse = oci_parse($conn, $surfaceSumSql);
oci_bind_by_name($surfaceSumParse, ":projName", $_SESSION['cd-dropdown']);
oci_define_by_name($surfaceSumParse, "SURFACETOTAL", $totalSurfaceSum);
oci_execute($surfaceSumParse);
<div class="small-box bg-green">
<div class="inner">
<h3>
<?php echo number_format($totalSurfaceSum,2); ?><sup style="font-size: 20px">M2</sup>
</h3>
<p>
TOTAL PROJECT SURFACE
</p>
</div>
</div>
My question is how do i make the software refreshes the content of the every 1 second so that the user will see the value changes over time. Thanks in advance for your help.

how to load specific files/urls based on a database query

I am trying to alter a piece of code that will allow me to position an image left, right or centred.
I have been able to create radio buttons that populate a table on the database but now I want to pull that information out and create an IF statement that will then open a specified page depending on the result.
For example, If I choose the 'right' radio button, this will then populate the database with the id and the 'right' value. I then want to pull the value from the database so if the value is 'right' the newsitem2.php page will load.
This is the code I have in order to do this:
if ("" . $tnrow['newsimage'] . "" == "none") {
}else {
$radio="SELECT imageposition FROM newsimage";
if ("$radio" == Centre) {
<p class='newsmore'> <a href='newsitem3.php?i=" . $tnrow['niid'] . "'>Read More</a></p>
}
if ("$radio" == Right) {
<p class='newsmore'> <a href='newsitem2.php?i=" . $tnrow['niid'] . "'>Read More</a></p>
}
if ("$radio" == Left) {
<p class='newsmore'> <a href='newsitem.php?i=" . $tnrow['niid'] . "'>Read More</a></p>
}
However, it is not loading each page it just loads the 'newsitem' page with the image on the left. is there any reason for this?
You don't make any call to your database, so $radio is just a String when you use it for now (representing the query) ; you have to send this query to your database (using for example a wrapper like PDO), and then get the result back.
Another thing, are Right, Left, ... constants or enum values ? 'Cause if they're not, you also have to put them into quotes, in order to be interpreted as String
There are a lot of weird instructions in your code, you should probably get more informations on PHP, 'cause you doesn't really seem to understand the concepts for now. Get a good tutorial/book, and you'll see that what you want to do is pretty easy when you got the ideas behind. ;)
Your IF's are all wrong. I assume the value of $radio is a string?
if ("$radio" == Centre) {
Should be
if ($radio == 'Centre') {
And so on for all the others

Check if there are new messages with ajax

I have put up some code to make an ajax, php, mysql chat in real time...
this is my code:
-jQuery/Ajax
$(function(){
setInterval(function() {
$('#chMsgCont').load('./ajax/msg.php');
var div = $('#chMsgCont');
var o = div.offset().top;
div.scrollTop( o + 12302012 );
}
,1000);
});
"./ajax/msg.php"
<?php
include("../system/config.site.php");
$query = mysql_query("SELECT * FROM chat_msg ORDER BY timestamp ASC");
while($p = mysql_fetch_assoc($query)) {
$auth = mysql_fetch_assoc(mysql_query("SELECT * FROM chat_users WHERE id = '".$p['auth_id']."'"));
?>
<div class="chatMsg">
<p id="chatPMsg">
<span class="chatTime"><?php echo date("H:i", $p['timestamp']); ?></span>
<b><?php echo $auth['name']." ".$auth['surname']; ?></b><br />
<?php echo stripslashes($p['msg']); ?>
</p>
<p id="chatImg">
<img src="./images/thumb<?php echo $p['auth_id']; ?>.png" />
</p>
<div style="clear:both;"><!– –> </div>
</div>
<?php
}
?>
I haven't found any method for not loading every second the "msg.php" file... I thought: is it possible to load initially the messages and then with setIntervall check for new messages to append to #chMsgCont? I think it is possible, but i can't figure out how to code it :/, can anyone help me?
Yes it is possible to load all messages upon initial page load; then subsequently update the chat window with new messages (have you ever wondered how Gmail auto populates new mail into your Inbox without a page refresh? Lots of AJAX requests).
You're on the right track. Just make sure you have a column in your MySQL database that holds a "yes/no" value on whether its a new message or not. Then everytime a new message is fetched using ajax/msg.php, in addition to fetching and outputting the content, perform a SQL update to update the fetched data as well.
Once you have that working you can further optimize your chat application by creating a simplier MySQL SELECT statement that simply checks the count of "new" rows (SELECT COUNT(new_message) FROM messages). Then check the value returned and whether it's greater than 0 before attempting to append content. I would also check for empty return data in your JavaScript to prevent from unnecessarily manipulating DOM elements.

PHP data retrieval from database and display

How do I Limit some information displayed from the database and add a link eg "More" to enable read all information in a drop down using PHP. such as what is on facebook (Read more...). I am dealing with a lot of content and I dont want it all displayed at once.
Here is part of the code
echo "<p>".$row['Firstname']." ".$row['Lastname']."</p>";
echo "<p>".$row["Course"]." | ".$row["RegID"]."</p>";
echo "<p>".$row["Email"]."</p>";
echo "<p>"."Tel:".$row["Telephone"]."</p>";
echo "<p>".$row["info"]."</p>";
The code is running well only that I want to limit the information
echo "<p>".$row["info"]."</p>";
so that not all is displayed
Thanks
Use Jquery-ui click on "view source" and you'll see it's very simple really, just set the row that you want as the header (what's clicked to show the rest) and store the rest in a div below.
Split info into two strings, one intro, and the rest. Display only the intro to begin with. Insert a link that displays the rest when clicked.
$intro = substr($row['info'], 0, 200);
$rest = substr($row['info'], 200);
echo sprintf(
<<<HTML
<p>
<span class="intro">%s</span><span class="rest" class="display: none">%s</span>
Show more
</p>
HTML
, htmlentities($intro)
, htmlentities($rest)
);
displayRest is a Javascript-function that, given a link, finds the previous span with class rest, shows it and removes the link. I leave it as an exercise to implement this in a way that fits your project. You can go with native Javascript, or use a library such as jQuery, YUI, MooTools, Prototype etc.
if(isset($_POST['more']))
{
$query="select col1,col2,col3, ... ,colN from tableName ";
}
else
{
$query="select col1,col2,col3 from tableName ";
}
//HTML
<form method="post">
<input type="submit" name="more" value="More" />
</form>
//PHP
$records=mysql_query($query);
while($row=mysql_fetch_assoc($records))
{
//Display
}
The limit must be fixed on the SQL request.
// If you want to transmit limitation with a GET PARAMETER.
// You can also $_POST ur data.
$limitation = $_GET['limit'];
//..... And in your SQL REQUEST
$sql = "SELECT * FROM your_table LIMIT 0 , $limitation";
//And in the link....
echo 'Show only 10 Results'
?>
You can optimize that and add security precaution to prevent errors when $limitation receive empty or non numeric parameters.
<?php if(isset($_GET['limit']) && !empty($_GET['limit']) && !preg_match(EXPRESSION, $_GET['limit'])){
//YOU CAN DO THE LIMITATION WHITOUT SQL ERRORS
}
else{
//ERROR DIRECTIVE
}
?>

Categories