including file with if else condition - php

i am including payment.php but only in case of if image type is equal to "map" else rest of the html content without including payment.php.
for Example
<?php
include('aaa.php');
$url=$_GET['url'];
$sql = "SELECT * FROM maps WHERE url='$url'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$name=$row['name'];$metaTitle=$row['metaTitle'];$metaDesc=$row['metaDesc'];$metaKeyword=$row['metaKeyword'];$imageType=$row['imageType'];
?>
some content appears. i need to write php code for below line
if imageType(name of field in table)= map
then include external file "payment.php"
else
html content without indluding payment.php file.

if($imageType == "map") {
include('payment.php');
}
//html content here

Related

PHP's include not working properly

So recently I started a project where I need to load some files. In these files I have colors that come via SQL;
The Problem is, I can only include one color in a file where they'll be used. The first file to be included will work properly, but the second one will not.
The weirdest thing is that if I try to add some echo, all included files will echo it, but only the first one in the order of the import will actually work. It's kind of complicate to explain, but some code can help:
Portifolio.php (where I will use the colors)
<?php
error_reporting(E_ALL);
include_once ('php/db.php');
include_once ('php/colors/accent.php');
include_once ('php/colors/primary.php');
$page =
<<<HTML
// Long HTML code here
<nav class="$primarycolor">
// More code
<a class ="$accentcolor">
accent.php:
<?php
include ('../db.php');
$sql = "SELECT id, color FROM Colors WHERE id = 2";
$getcolor = mysqli_query($conn, $sql);
echo "Accent.php included";
if ($getcolor->num_rows > 0) {
while($row = $getcolor->fetch_assoc()) {
$accentcolor = $row['color'];
}
}
mysqli_close($conn);
?>
primary.php
<?php
include ('../db.php');
$sql = "SELECT id, color FROM Colors WHERE id = 1";
$getcolor = mysqli_query($conn, $sql);
echo "Primary.php has been included";
if ($getcolor->num_rows > 0) {
while($row = $getcolor->fetch_assoc()) {
$primarycolor = $row['color'];
}
}
mysqli_close($conn);
?>
db.php
<?php
$conn = mysqli_connect("localhost","mucap","pswd","webdata");
echo "DB included.";
if (mysqli_connect_errno())
{
echo "Error: " . mysqli_connect_error();
}
?>
In this case:
No errors are gonna be displayed.
$accentcolor will work
$primarycolor will simply pretend it does not exists
All files will echo what they were told to.
I did a lot of research and figured out it could be something with file paths, but I dont think so: Here goes (part of) my file structure:
I cant do [$DOCUMENT_ROOT] as this is not supposed to have a fixed path, long explanation.
What should I do?
Try replacing your statement with this:
if ($getcolor) {
while($row = $getcolor->fetch_assoc()) {
$primarycolor = $row['color'];
}
} else{
echo"no results in row";
}
The simple if check is possible because the results of a search return "FALSE" if there's no data in the results.
The else statement is required just for verification purposes.
If you only expect 1 result, you can do "LIMIT 1" as well as eliminate the while statement.
If you don't want the script to run at all, you should use require_once, not include_once

How to get information from database file, and retrieve that information in the website file?

I'm in the process of doing a "mock" website for a class and I'm having issues getting the information from my database file, to the actual page in my site. I should mention that I'm a total php newb. Also there isn't any security yet.
I'm calling the connection from another file, so I didn't include that.
database function:
function GetProductsByCategory($categoryID) {
global $conn, $productsResult;
echo $categoryID;
$sql = "SELECT * FROM products WHERE prodCategory = '$categoryID'";
$productsResult = $conn->query($sql);
}
Website File:
//In the head of website file
<?php
$categoryID = "87";
if (isset($_GET['category'])) {
GetProductsByCategory($categoryID);
} else {
// Fallback behaviour goes here
}
?>
//In the body
<?php while ($row = $productsResult->fetch_assoc()) {
//Just trying to get information from the database file
echo '<div>'.$row["prodName"].'</div>';
}
?>
I should mention that nothing throws an error, It echos out the category ID at the top, but inside the <div> it just doesn't show anything.
Call the function and save the result in a variable:
$result = GetProductsByCategory($categoryID);
then inside the function:
$productsResult = $conn->query($sql);
return $productsResult;
then your while loop should use this returned result (i.e., $result):
while ($row = $result->fetch_assoc()) { ... }

download button - save file to disk instead of opening

I have a download button and when i click on it, instead of saving to disk it opens it in the browser. I tried a bunch of attempts to make it open in the browser but it doesnt seem to do anything
<?php
// make a connection to the database
require_once("connection/connection.php");
//retrieve the ID from the url to fetch the specific details
if ($_GET['id'] != ""){
$item_id = $_GET['id'];
$bad_id = FALSE;
}
else{
$item_id = "";
$bad_id = TRUE;
}
//select the specific item from the database
// run if statement to ensure valid id was passed
if (is_numeric ($_GET['id'])){
$query = "SELECT name FROM repository WHERE item_id = '$item_id'";
$result = mysql_query($query) or die(mysql_error());
// assign the values to an array
$row = mysql_fetch_assoc($result);
//assign the values from the array to variables
$name = $row['name'];
}
// define path to the xml file
$file = "xml/".$hud_name . "_cfg.xml";
// check to make sure the file exists
if(!file_exists($file)){
die('Error: File not found.');
} else{
// Set headers
header("Content-Type: application/xml");
header("Content-Disposition:attachment; filename=".basename($file)."");
readfile($file);
}
?>
That is download.php and it obviously finds the file because it doesnt give the error about it not existing. It also echos back the correct file path
Then on another page i have:
<img src="images/download.png" alt=""/>
Any ideas whats wrong?
Well the solution turned out to be simple in the end but i didnt see any documentation saying the header must be the very first line. If i placed:
header("Content-Type: application/xml");
as the first line and then the coding below it and the other header info at the end it works. Im not sure if that's the solution or a workaround but it fixed it for me

Viewing Image from database

I have a file in which a user can view its stored files. I want that only the logged in user and it can only be viewed by that member who has stored that file. It works fine when I view other files like .html, .txt etc. But when I view any image, it doesn't work.
This is my script :
require ('config.php');
$id = intval($_GET['id']);
$dn2 = mysql_query('select authorid from files where uploadid="'.$id.'"');
while($dnn2 = mysql_fetch_array($dn2))
{
if(isset($_SESSION['username']) and ($_SESSION['userid']==$dnn2['authorid'] or $_SESSION['username']==$admin)){
$query = "SELECT data, filetype FROM files where uploadid=$id"; //Find the file, pull the filecontents and the filetype
$result = MYSQL_QUERY($query); // run the query
if($row=mysql_fetch_row($result)) // pull the first row of the result into an array(there will only be one)
{
$data = $row[0]; // First bit is the data
$type = $row[1]; // second is the filename
Header( "Content-type: $type"); // Send the header of the approptiate file type, if it's' a image you want it to show as one :)
print $data; // Send the data.
}
else // the id was invalid
{
echo "invalid id";
}
}
}
How can I view the image?

From PHP to Javascript

I have the following code on my PHP webapp, it uses PHP to look for products in a Sqlite3 Databse file locally. I was tryng to convert it into an Android App with Phonegap, but sadly Phonegap does't work with PHP, so I thought I could make a JavaScript file replacing the PHP.
However, I can't get it to work. In case what I am trying to do is impossible, I guess there should be a way to execute the PHP files on the server and just display the results on the app.
($_GET['busq']){
$busq=$_GET['busq'];
function ultlet($cadena) {
$cant = strlen($cadena);
$cant--;
$let = substr($cadena, $cant);
if ($let == "s") {
$cadena = substr($cadena, 0, $cant);
}
return $cadena;
}
str_replace("-",$busq," ");
$arreg=explode(" ",$busq);
$cuent=count($arreg);
for($a=0;$a<$cuent;$a++){
$arreg[$a] = ultlet($arreg[$a]);
}
$query="SELECT * from MyTable WHERE";
for($i=0;$i<$cuent;$i++){
if ($i>=1){
$query=$query." AND ";
}
$query=$query." (col1 LIKE \"%$arreg[$i]%\" OR col6 LIKE \"%$arreg[$i]%\") ";
}
$db = new PDO('sqlite:MyDatabase.sqlite');
$result = $db->query($query);
foreach ($result as $row) {
if ($row['codigo_cat']!=null){
$class = 'class="image"';
}else{
$class = 'class="row"';
}
echo '<div '.$class.' alt="'.$row['col4'].'">';
echo 'Codigo: '.$row['col2']." / ".$row['col3']."<br>";
echo 'Nombre: '.$row['col1']."<br>";
echo '</div>';
}
}
There is also another file wich based on the user's input on a search bar creates the variable 'busq' which is used to display the products and its prices.
Modify the PHP script to output its contents as an xml file, and use javascript's XMLHttpRequest function to create a request to the file, and get the information you need.

Categories