Page 1 of 1

Combine two PHP files into 1

Posted: Thu Jun 15, 2006 6:24 am
by JimiH
Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, I'm a total noob at PHP & MySQL

I have two PHP files

First has an upload form for files a user selects, this is added to the DB, this works fine.

Second  has two drop down lists for selecting catagories this also works fine

I would like to combine the two files into one so I have the browse field & the catagorie selection field
on the same page.

File 1

Code: Select all

<html>
<head>
<link rel="stylesheet" type="text/css" href="/styles/yourfile.css"> 

<title>Upload File To MySQL Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
// you can change this to any directory you want
// as long as php can write to it

$uploadDir = 'C:/websites/user_login/uploads/'; 


echo "Select your story <b>$session->username</b>, then click upload. <br>";

if(isset($_POST['upload']))
{
    $fileName = $_FILES['userfile']['name'];
    $tmpName  = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];

// get the file extension first
	$ext      = substr(strrchr($fileName, "."), 1); 
	
	// generate the random file name
	$randName = md5(rand() * time());
	
	// and now we have the unique file name for the upload file
    $filePath = $uploadDir . $randName . '.' . $ext;


    // the files will be saved in filePath 
   // $filePath = $uploadDir . $fileName;



    // move the files to the specified directory
    // if the upload directory is not writable or
    // something else went wrong $result will be false
    $result    = move_uploaded_file($tmpName, $filePath);
    if (!$result) {
        echo "Error uploading file";
        exit;
    }
    
    include 'config.php';
    include 'opendb.php';

    if(!get_magic_quotes_gpc())
    {
        $fileName  = addslashes($fileName);
        $filePath  = addslashes($filePath);
	  $description=$_POST['description'];
    }  

    $query = "INSERT INTO upload2 (name, size, type, path, description, AuthorID ) ".
             "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$description', '$session->username')";

    mysql_query($query) or die('Error, query failed : ' . mysql_error());                    

    include 'closedb.php';
    
    echo "<br>File uploaded<br>";
/* Link back to main */
echo "<br>Back To [<a href=\"main.php\">Main</a>]<br>";

}        
?>
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
  <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr> 
      <td width="246"><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile" type="file" class="box" id="userfile">
         </td>
      <td width="80"><input name="upload" type="submit" class="box" id="upload" value="  Upload  "></td>
    </tr>
  </table>
<br>Description: <input type="text" name="description"><br> 



<?
End

File 2

Code: Select all

<?php

    include 'config.php';
    include 'opendb.php';
?>

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>

<link rel="stylesheet" type="text/css" href="/styles/yourfile.css"> 
<title>Upload File To MySQL Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">

</head>



<SCRIPT language=JavaScript>
function reload(form)


{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='dd.php?cat=' + val ;
}

</script>


<body>
<?


///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category"); 
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory///// 
if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory"); 
}else{$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory"); } 
////////// end of query for second subcategory drop down list box ///////////////////////////

echo "<form method=post name=f1 action='dd-check.php'>";
/// Add your form processing page address to action in above line. Example  action=dd-check.php////
//////////        Starting of first drop downlist /////////
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) { 
if($noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
else{echo  "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
}
echo "</select>";
//////////////////  This will end the first drop down list ///////////

//////////        Starting of second drop downlist /////////
echo "<select name='subcat'><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) { 
echo  "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
}
echo "</select>";
//////////////////  This will end the second drop down list ///////////
//// Add your other form fields as needed here/////
echo "<input type=submit value=Submit>";
echo "</form>";
?>
<center><a href='http://www.plus2net.com'>PHP SQL HTML free tutorials and scripts</a></center> 
</body>

</html>
End

Sorry for the long post

Geoff


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Jun 15, 2006 6:32 am
by JayBird
Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

If i could spend more time answering questions rather than cleaning up peoples post becuase they cant read the posting guidelines, then we would ALL get a lot more done!

Posted: Thu Jun 15, 2006 6:54 am
by Chris Corbyn
You mean you want both files to act as one signle file? Just include() one file into the other, or make a third file that include()'s both files.

Posted: Thu Jun 15, 2006 7:17 am
by tecktalkcm0391
Your could do something like this

If page.php?id=1 is the url of the page then page one will show.
If page.php?id=2 is the url of the page then page two will show.
If page.php is the url of the page, then an error message will show.

Code: Select all

<?php
//Keep everything in memory
ob_start();

// Tests to see if the ID is there to GET
if(!isset($_GET['id'])){
// If no id to get
    print "ERROR, no page id.<br>Page could not be found.";
    // Flush memory 
    ob_flush();
    exit;

//Othewise
} else {
// Gets the Variable:
     $id = $_GET['id'];
}
// If id is = to i
if($id == 1){
     include("page 1.php");
// Otherwise if id = 2
} elseif($id = 2){
     include("page 1.php");
} else {
     print "ERROR, page can not be found.";
}

// Flush memory 
ob_flush();

?>

Posted: Thu Jun 15, 2006 7:49 am
by JimiH
d11wtq wrote:You mean you want both files to act as one signle file? Just include() one file into the other, or make a third file that include()'s both files.
Hi Sorry for not reading the rules :oops:

Code: Select all

<?php


include 'dd.php';
include 'upload2.php';

?>
This looks fine at first, however the 'upload2.php' disapears from the page after I have selected a value from the dropdown fields from dd.php.
This is due to the page reloading after a value is picked from dropdown field one

I really just neet to get the two dropdown fields from dd.php onto upload2.php with the description field & the Browse & Upload buttons.

I've been trying for an hour to get this to work.

Thanks

Geoff

Posted: Thu Jun 15, 2006 9:43 am
by JimiH
Hi All

Fixed this refresh problem

Code: Select all

<SCRIPT language=JavaScript>
function reload(form)

{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='test.php?cat=' + val ;
}

</script>
Changed self.location to the new php file ='test.php?cat=' + val

I was refreshing to the old dd.php. 'dd.php?cat=' + val

Thanks

Geoff