Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Combine 2 php files

Posted on 2006-10-26
5
Medium Priority
?
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Introduction to Processes

650 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