Solved

Combine 2 php files

Posted on 2006-10-26
5
194 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
5 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

948 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now