[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Combine 2 php files

Posted on 2006-10-26
5
Medium Priority
?
201 Views
Last Modified: 2010-04-16
Is it possible to combine these 2 php files?

<?
mysql_connect("localhost","user","pass");
mysql_select_db("database");
$submit = $_POST['submit'];
$name = $_POST['name'];
$time = $_POST['time'];
$message = $_POST['message'];
if($submit){
$result=MYSQL_QUERY("INSERT INTO shoutbox (id,name,time,message)"."VALUES ('NULL','$name','$time','$message')");
} else {
}
?>

====================
and
====================

<?php
header("Content-type: text/xml");

$host = "localhost";
$user = "user";
$pass = "pass";
$database = "database";

$linkID = mysql_connect($host,$user,$pass) or die ("Could not connect to host.");
mysql_select_db($database,$linkID) or die ("Could not find database.");

$query = "SELECT * from shoutbox ORDER BY id DESC";
$resultID = mysql_query($query,$linkID) or die ("Data not found");

$xml_output = "<?xml version =\"1.0\"?>\n";
$xml_output = "<shouts>\n";
for ($x = 0; $x < mysql_num_rows($resultID); $x++){
      $row = mysql_fetch_assoc($resultID);
      $xml_output .= "\t<shout>\n";
      $xml_output .= "\t\t<name>" . $row['name'] . "</name>\n";
      $xml_output .= "\t\t<time>" . $row['time'] . "</time>\n";
      $xml_output .= "\t\t<message>" . $row['message'] . "</message>\n";
      $xml_output .= "\t</shout>\n";
}
$xml_output.= "</shouts>";

echo $xml_output;
?>
============================

I'm trying to create a flash-based shoutbox dealy, but with the two files seperated, when the flash does the POST of the message, it re-directs to the post.php page, which is not the desired effect. I need for the informaton to get posted and then refresh the flash movie with the new post added.

Any help would be greatly appreciated.

Thanks.
0
Comment
Question by:warts
3 Comments
 
LVL 5

Expert Comment

by:daohailam
ID: 17813884
Like this:

<?php
header("Content-type: text/xml");

$host = "localhost";
$user = "user";
$pass = "pass";
$database = "database";

$linkID = mysql_connect($host,$user,$pass) or die ("Could not connect to host.");
mysql_select_db($database,$linkID) or die ("Could not find database.");

$query = "SELECT * from shoutbox ORDER BY id DESC";
$resultID = mysql_query($query,$linkID) or die ("Data not found");

$xml_output = "<?xml version =\"1.0\"?>\n";
$xml_output = "<shouts>\n";
for ($x = 0; $x < mysql_num_rows($resultID); $x++){
     $row = mysql_fetch_assoc($resultID);
     $xml_output .= "\t<shout>\n";
     $xml_output .= "\t\t<name>" . $row['name'] . "</name>\n";
     $xml_output .= "\t\t<time>" . $row['time'] . "</time>\n";
     $xml_output .= "\t\t<message>" . $row['message'] . "</message>\n";
     $xml_output .= "\t</shout>\n";
}
$xml_output.= "</shouts>";

echo $xml_output;

$submit = $_POST['submit'];
$name = $_POST['name'];
$time = $_POST['time'];
$message = $_POST['message'];
if($submit){
$result=mysql_query("INSERT INTO shoutbox (id,name,time,message)"."VALUES ('NULL','$name','$time','$message')", $linkID);
} else {
}
?>
0
 
LVL 1

Author Comment

by:warts
ID: 17814031
Thanks,

Unfortunately, that's not going to work. It does the same thing as it was doing before. When I hit the submit button it does the post and redirects to the php page instead of staying within the flash movie. If I try to embed the flash into the php page with the combined scripts, it just hands out xml not well-formatted errors.

The flash movie is using 2 urls. 1 is for getting the posts that are already there, the other is for posting new messages to the databse.

I was hoping that there was a way to combine the 2 scripts in a way that allowed the movie to both post and view from within the same php file, and within the .swf file.

I'd ideally like to be able to go directly to the swf url and have it work, instead of a php or html page. This way I can just embed the swf into the site I'm working on.

Thanks again for helping.
0
 
LVL 1

Accepted Solution

by:
davidisaac earned 2000 total points
ID: 17822626
Do you mean like this?

Then you would just have to change the flash movie url to append "?case=insert" for the insert function.

<?

$host = "localhost";
$user = "user";
$pass = "pass";
$database = "database";


$case = $_GET['case'];


switch( $case ) {
    case 'insert':
        insertIntoDB();
        break;
    default:
        showResults();
        break;
}

exit;


function insertIntoDB() {
  global $host, $user, $pass, $database;

  mysql_connect("localhost","user","pass");
  mysql_select_db("database");
  $submit = $_POST['submit'];
  $name = $_POST['name'];
  $time = $_POST['time'];
  $message = $_POST['message'];
  if($submit){
  $result=MYSQL_QUERY("INSERT INTO shoutbox (id,name,time,message)"."VALUE('NULL','$name','$time','$message')");
  } else {
  }
}



function selectResults() {
  global $host, $user, $pass, $database;

  header("Content-type: text/xml");

  $linkID = mysql_connect($host,$user,$pass) or die ("Could not connect to host.");
  mysql_select_db($database,$linkID) or die ("Could not find database.");

  $query = "SELECT * from shoutbox ORDER BY id DESC";
  $resultID = mysql_query($query,$linkID) or die ("Data not found");

  $xml_output = "<?xml version =\"1.0\"?>\n";
  $xml_output = "<shouts>\n";
  for ($x = 0; $x < mysql_num_rows($resultID); $x++){
     $row = mysql_fetch_assoc($resultID);
     $xml_output .= "\t<shout>\n";
     $xml_output .= "\t\t<name>" . $row['name'] . "</name>\n";
     $xml_output .= "\t\t<time>" . $row['time'] . "</time>\n";
     $xml_output .= "\t\t<message>" . $row['message'] . "</message>\n";
     $xml_output .= "\t</shout>\n";
  }
  $xml_output.= "</shouts>";

  echo $xml_output;
}
?>
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Six Sigma Control Plans

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question