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

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

Related

Not able to Display without storing it in DB

I am not sure whether I am displaying the image in the right way or not. I am not able to display the image in the given field and the images are not stored in DB ...it's a gender based avatar choosing script (I hope so).
if (isset($_GET['gender']) == 'm') {
echo ' <img id="avat" src="imgs/avatar-boy-1.png" alt="">';
} elseif (isset($_GET['gender']) == 'f') {
echo ' <img id="avat" src="imgs/avatar-girl-1.png" alt="">';
}
Funk Forty Niner and Dharman pointed this out in the comments, but isset($_GET['gender'])=='m' doesn't make sense.
isset($_GET['gender']) returns a boolean, true or false, telling you whether $_GET['gender'] is set. You can't meaningfully compare that boolean to 'm'.
You probably want something like isset($_GET['gender']) && $_GET['gender'] == 'm'.
Or just move the isset($_GET['gender']) into its own if statement, since you are checking it both times.

Creating tr value filter

I creating filer for website data. I have created value attribute for <tr>which means that the row belongs for specific company. And now i want to make that table would show rows depending which value i will choose. And i really dont have idea how to do that. $currentCompany is that value number of company id. Here is my script:
if($row["type"] != "Staff Comment")
{
print("<tr value=". $currentCompany ." style='height:25px;'>\n");
for ($loop = 0; $loop < count($fields["headings"]); $loop++) {
print("<td bgcolor='white' align=" . $fields["aligns"][$loop] . ">\n");
//translate some information, some not
if ($fields["headings"][$loop] == "Status") {
print($AppUI->_(format_field($row[$fields["columns"][$loop]], $fields["types"][$loop], $row[$fields["columns"][0]])) . "\n");
}
else {
print(format_field($row[$fields["columns"][$loop]], $fields["types"][$loop], $row[$fields["columns"][0]]) . "\n");
}
print("</td>\n");
//print("<tr>" . var_dump($fields) ."</tr>\n");
}
print("</tr>\n");
And here is how it looks like in website:
Hope you can help me guys. For example if i would choose value 25 it would print all tr which has that value.
You want to hide rows, or just not print them? If hide - You have to use JS - PHP cannot modify site dynamically. If just don't print them - use simple if, and possibly You want to pass this information via simple form and POST request.
Create form, which action point to Your script, and have text field where you can specify which company have to be shown. Then, in Your code something like this:
if($currentCompany == $_POST['company'])
{
print("<tr value=". $currentCompany ." style='height:25px;'>\n");
//More code....
}
With JS it will be more complicated - but easy, too. It will have to be done in this way:
Get all TR's
Check every TR value. If value not corresponds to Your needs - add "display: none" CSS attribute.
Concrete JS implementation depends on way you want to code in JS - pure JS, jQuery, other JS framework.

Delete entry from page but keep in DB (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.

How do i pass a mysql query result as a variable to be used in another query search?

Ok so basically I've got a product database. members search the database by clicking on categories of their choice.
These categories are spans which all trigger a javascript (ajax) function which calls a php query to find all of the subcategories of the span just clicked.
Those subcategories are outputted in a while loop which creates more spans which also trigger the same js function which will link back to the query.
The code below shows the output of the categories displayed based on the output of the database query.
"<div id=\"div1\">";
while($row = mysql_fetch_array($result))
{
$spans++;
echo " <span id=\"$spans\"
onclick='serverconnect(\"http://localhost/dreamweaver/Website/webdev/Code structure.php\",
\"nya\")'> $row[1] </span>";
echo "<br />";
}
"</div>";
So let's say the output of this is:
HEALTH
FITNESS
EDUCATION
LEISURE
How could i pass the span names as values for the next query so that whatever span is clicked on gets its appropriate value passed to the database search?
Would i have to somehow assign the contents of each span to a variable and then pass the variable to the javascript function which would then pass it to the query?
Im out of ideas as to how to do that, or if i should do it another way.
Thanks!
Try replacing:
echo " <span id=\"$spans\"
onclick='serverconnect(\"http://localhost/dreamweaver/Website/webdev/Code structure.php
\", \"nya\")'> $row[1] </span>";
With:
echo " <span id=\"$spans\"
onclick='serverconnect(\"http://localhost/dreamweaver/Website/webdev/Code
structure.php?value=" . urlencode($row[1]) . "\", \"nya\")'> $row[1] </span>";
You may then retrieve the value in $_GET['value'] after clicking the SPAN.
Note: I added some line breaks in the text for ease of reading, don't forget to remove them.

Using MySQL Databases and PHP to Populate forms

I am contemplating taking the next step with my PHP applications and making the option fields dynamic. That would open the doors for more automation.
I have drop downs throughout my project, they are used to select a specific user and I update them manually when a new user is added (which is also a manual process). But if i take the first step and make these drop downs become populated by a MySQL Database, then i can move on to dynamic user creation.
I know how I can achieve this, but I am curious about some other alternatives (If there is any).
Here is what I would do..
$query = ** MySQL Select * From Database Query **
echo '<select name="usernames">';
while($row == mysql_fetch_array($query))
{
echo '<option>' . $row['username'] . '</option>';
}
echo '</select>';
So my questions is, would you do this differently? And why? Thanks!
What you are doing will work fine. I like to make it into a function so that if I ever need that dropdown on another page I dont have to write a lot of code over again.
function userDD()
{
$query = ** MySQL Select * From Database Query **
$html = '<select name="usernames">';
while($row == mysql_fetch_array($query))
{
$html .= '<option>' . $row['username'] . '</option>';
}
$html .= '</select>';
return $html;
}
This code does exactly what your code does except it doenst use echo. Instead you use a variable ($html) to store all of the data then when you are done you return it.
Your way is fine, but two things need to be changed:
- Run htmlentities() or htmlspecialchars() on all echoed HTML to avoid XSS. Unless you already sanitized it at database entry time but I find this practice silly.
- Add a value attribute to each <option> tag, otherwise you won't be able to retrieve the username selected. I suggest using the username's corresponding ID or something else that's unique to that user. If it's a string, use htmlentities/htmlspecialchars on it too.
php file
$users = getUsers();
include('template.tpl');
template
<select name="username">
<?php foreach( $users as $user ): ?>
<li><?= e( $user['username'] ) ?></li>
<?php endforeach; ?>
</select>
e is a function that escapes strings to prevent xss attacks
I wouldn't put an SQL query in the same document as my output...
I'd create a document containing all SQL queries, in functions, and include that file. Just to keep things seperated.

Categories