?
Solved

Combine 2 php files

Posted on 2006-10-26
5
Medium Priority
?
199 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Progress
Suggested Courses

743 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