Showing PHP in a PhoneGap based App - php

I'm trying to show the results of the php in my app, when I test it outside of the app, it works fine, and I have tried many solutions out there to load it with js, ajax, etc. but can't get it to work. Any ideas?
The php gets data from MySql database
-----select.php------
<?php
$con=mysqli_connect("localhost","localhost","","test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
/*while($row = mysqli_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}*/
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

Phonegap doesn't have any concept of PHP, you are unable to run PHP in a PhoneGap application. You will need to make AJAX requests from your application to your server for interaction.
The reason it works fine out of the app, is that you're probably running it in the browser straight on the server.
Take a look at jQuery if you're new to it all, especially $.ajax().

Phone gap does not support php, so will be good, if you want non static content... if you have a API which it will provide the content for you application. epiphany it is a really good open source framework created by Jaisen. This will give you an good start to create a good API for it. You can also learn about Zend Frameworks which it is more robust for big applications.

Related

Is it possible to get a URL from a JSON file to display an image from img src

(note: new to json) I have a JSON file that contains image URLs inside of it (https://pastebin.com/TavfuziF), with the following code:
<!DOCTYPE html>
<html>
<body>
<center>
<?php
$jdata = file_get_contents("cats-in-moviesnew.json");
//decode the retrieved json file into an array
$contents = json_decode($jdata, true);
echo
//create the table
"<table style border=3px solid black; ><tr><th>Producer</th><th>Director</th><th>Title</th><th>Year made</th><th>URL</th><th>Image</th><th>Poster URL</th></tr>";
//filling the table
foreach($contents as $record) {
echo
"<tr>
<td>" . $record["fields"]["produced_by"] . "</td>
<td>" . $record["fields"]["directed_by"] . "</td>
<td>" . $record["fields"]["title"] . "</td>
<td>" . $record["fields"]["year"] . "</td>
<td>" . $record["fields"]["url"] . "</td>
<td>" .
$record["fields"]["url_poster"]
. "</td>
<td>" . $record["fields"]["url_poster"] . "</td>
</tr>";
}
?>
</body>
</center>
</html>
and Ive been trying to figure out if its possible to do something like <img src="$record["fields"]["url_poster"]"> (currently where the gap is in the table filling bit) inside that code is possible or not. Ive tried several things which haven't worked for me, Ive seen things talking about converting stuff to base64 but it's very complicated for me at my level, and I don't know what else to do at this point. Any help would be greatly appreciated.
nevermind I figured it out just needed to add {} before the $record["fields"]["url_poster"], so it looks like: "<img src={$record["fields"]["url_poster"]}>"
You can do it like this:
echo '<table>';
foreach($contents as $record){
echo '<tr>';
echo '<td><img src="'.$record['fields']['poster_url'].'" /></td>';
echo '</tr>';
}
echo '<table';
and just a piece of advice, use indentation on you code so in the feature someone handle the code it is more easier to read and easier to debug. :)

Add sqlite entry Using PHP

I am running a simple code here to insert a set of entries in a sqlite table.
class MyDB extends SQLite3
{
function __construct()
{
$this->open('new_test.db');
}
}
echo "opened database succesfully";
$db = new MyDB();
$db->exec("INSERT INTO RECORDSS(ID,NAME,DEVICE,PROJECT,IP,COMMENT) VALUES('$id','$name','$device','$project','$ip','$comment');");
$ret = $db->exec($sql);
if(!$ret) {
echo "No error in if statement";
echo $db->lastErrorMsg();
The program executes without any error. However when I open the data base. I see no entries in the table.
What I am doing wrong here ?
Here is my code to view the table.
class MyDB extends SQLite3 {
function __construct() {
$this->open('new_test.db');
}
}
$db = new MyDB();
$sql =<<<EOF
SELECT * from RECORDSS;
EOF;
$ret = $db->query($sql);
echo "<table style='width:100%'>
<tr>
<th> ID </th>
<th> NAME </th>
<th> DEVICE </th>
<th>PROJECT </th>
<th>IP</th>
<th>COMMENT</th>
</tr>";
while($row = $ret->fetchArray(SQLITE3_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['ID']. "</td>" ;
echo "<td>" . $row['NAME']. "</td>";
echo "<td>" . $row['DEVICE']. "</td>";
echo "<td>" . $row['PROJECT']. "</td>";
echo "<td>" . $row['IP']. "</td>";
echo "<td>" . $row['COMMENT']. "</td>";
echo "</tr>";
}
echo "Operation done successfully";
$db->close();
echo "</table>"
Couple of things that I have checked.
The table and database are created, in the same directory with executable permissions.
Tried to replace PHP function with python, to achieve the same objective.
Do I have to change something explicitly in the PHP, apache or sqlite Conf files ?
EDIT:- Adding a few details as asked in the comments:
SCRIPT:
called using <form action ="add_info.php" method="get">
The form contains:
`Name
<input type ="text" name ="name"/>
<br/>
Project
<input type ="text" name ="device"/>
<br/>
`
Not pasting all form fields to make it readable. They are similar.
PHP gets the variable as
$name= $_GET['name']
OS: Uuntu 16.04 server
FIle: All scripts stored under /var/www/html
I dont exactly know about 'file systems'
I got it working.
The issue was because of file permissions and ownership.
The apache process was run by user www-data. My files were were run under root. Hence giving write permissions to root did not help.
Here is what I did.
change the user of the directory to www-data using chown -hR www-data /var/www/html.
Gave read and write permissions to the database and PHP file.
Thanks for all the helpful comments.

How to make link of the database in PHP

Hi I want to show URL as a link in PHP the URL is shown by query from database but it is not a link so I want to make it link like using but I don't know what I am doing wrong
My data show like this in browser
ID Name URL
2 This localhost/p_uploads/00.jpg
3 Nissan localhost/p_uploads/7a.jpg
I want these URL's to be link so anyone can click on the url to open the image
Here is my PHP Code:
<?php
if(!isset($_COOKIE['loggedin'])){
header("location:index.php");
}
session_start();
if(!isset($_SESSION['user'])){
header("location: index.php");
}
else {
?>
<?php
$con=mysqli_connect("localhost","root","123","user");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"Select * from private_uploads where username = '".$_SESSION['user']."'")
or die(mysql_error());
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>URL</th>
</tr>";
while($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>",'<a href=' . $row['Link'] . '></a>',"</td>";
echo "</tr>";
}
echo "</table>";
//Views Counter
mysqli_close($con);}
?>
<?php
$url = htmlspecialchars($_SERVER['HTTP_REFERER']);
echo "<a href='$url'>back</a>";
?>
replace
echo "<td>",'<a href=' . $row['Link'] . '></a>',"</td>";
for this
echo '<td>[a name here would be nice]</td>';
you are missing the "" in the generated href
You're producing links that look like this:
That's both an invalid URL (unless you really do have a localhost/p_uplaods folder) and a link with no body, so you'll never see it.
If you want to access localhost the host, and not the folder, you should use use an absolute path on the current host, /p_uploads/00.jpg, or a fully qualified URL: http://localhost/p_uploads/00.jpg.
Your link looks like this:
It doesn't have a HTTP protocol. The link should look like this:
You might have a problem on this line:
echo "<td>",'<a href=' . $row['Link'] . '></a>',"</td>";
As you are referring to Link whereas in your database the column is called URL. Also add a text to the tag:
echo '<td>View</td>';

win32_query_Service_status

I am trying to connect to a remote server Win 2003 using a PHP script I have come across from php.net below and returns error 5 which is permissions, the code works great when I query localhost.
Has anyone ever come across this issue before or know a work around? Once I know I can connect I plan to monitor services and then be able to restart them with monitor script.
$servicename ="crmskidata";
$machine ="crm2011";
//print_r (win32_query_Service_status($servicename,$machine));
echo $servicename;
$sray = win32_query_Service_status($servicename,$machine);
echo "<br>";
print_r ($sray);
echo "<ul>";
echo "<li>Service Type: " .$sray[ServiceType] . "</li>";
echo "<li>CurrentState: " .$sray[CurrentState] . "</li>";
echo "<li>Controls Accpeted: " .$sray[ControlsAccepted] . "</li>";
echo "<li>Win32 Exit Code: " .$sray[Win32ExitCode] . "</li>";
echo "<li>Service Specific Exit Code: " .$sray[ServiceSpecificExitCode] . "</li>";
echo "<li>Check Point: " .$sray[CheckPoint] . "</li>";
echo "<li>Wait Hint: " .$sray[WaitHint] . "</li>";
echo "<li>Process Id: " .$sray[ProcessId] . "</li>";
echo "<li>Service Flags: " .$sray[ServiceFlags] . "</li>";
Added the webservers mavhine name to the local administrators group on the machine i was trying to connect to.

A php Noob in need of some local server help

So i installed XMAPP to view my php site im developing for a php mysql class at my local college and I ran aground hours ago and have been searching frantically for answers since. whenever i try to view my site all i see is this
" . $row['tid'] . ""; echo $id; $thisName = "\n
" . $row['fname'] . " " . $row['lname'] ."
"; echo $thisName; $description = "\n
" . nl2br($row['description']) . "
"; echo $description; echo ""; } include ("footer.php"); ?>
or something similar. we host on a local server that has everything we need but cannot access it from our homes. I was wondering if anyone could lend me a hand?
It looks like you have an echo statement where you're using a single quote to open a string but not close it. For instance:
echo '" . $row['tid'] . ""; echo $id; $thisName = "
Go search in your source for something like this. I'd bet that this is your error.

Categories