Friday, June 4, 2010

Creating a Search Function 2

In the last post, I mentioned how I would upload files to the server and collect their details into the db.
Here, I'll show you how I was able to search, format and display all the information:




$search=$_POST["search"];
$level = $_POST["level"];
$type = $_POST["type"];

//get the mysql and store them in $result
$result = mysql_query("SELECT * FROM Resources WHERE level LIKE '%$level%' AND (tags LIKE '%$search%' or name LIKE '%$search%') AND type LIKE '%$type%' ORDER BY date DESC ");


//grab all the content
while ($get_info = mysql_fetch_array($result)){
$series[] = array('name' => $get_info['name'], 'url' => $get_info['url'], 'tags' => $get_info['tags'], 'level' => $get_info['level'], 'submittedby' => $get_info['submittedby'], 'date' => $get_info['date']);
}

?>
<!-- Show Content -->
<p>Here are all the resources...</p>

<?php foreach ($series as $show): ?>
<blockquote>
<h2><?php echo htmlspecialchars($show['name'], ENT_QUOTES, 'UTF-8'); ?></h2>
<p><em><a href="http://www.tracyr.comlu.com/SFVT/upload/<?php echo htmlspecialchars($show['url'], ENT_QUOTES, 'UTF-8'); ?>">click to download file </a></em><br/>
Tags:
<em><?php echo htmlspecialchars($show['tags'], ENT_QUOTES, 'UTF-8'); ?></em><br/>
Submitted By:
<em><?php echo htmlspecialchars($show['submittedby'], ENT_QUOTES, 'UTF-8'); ?></em><br/>
Date added: <?php echo htmlspecialchars($show['date'], ENT_QUOTES, 'UTF-8'); ?>
</p></blockquote>
<?php endforeach; ?>





Basically, the code allows me to grab all of the content from the db- if I want to filter it using the search form, that's where this comes in:


WHERE level LIKE '%$level%' AND (tags LIKE '%$search%' or name LIKE '%$search%') AND type LIKE '%$type%'



The second part of the code allows me to format the results by fetching the array and mixing html and php to decide how each result displays. It also allows me to create the url of each file since they are all in the /upload folder and the filename comes from the database.

This all took me a little while to get working- especially understanding the fetch array and "for each..." sections. But got there in the end!

Creating a Search Function

One of the biggest projects of my site has been to create the ability for users to upload files, load them into a folder and their into a database, return the formatted results and format them.
Here are some of the things I've been working on:

Uploading Files

The first thing I did was create a form. The form posts to an external php file, collects inputted data about the file and there is a field that allows you to select a file (type:"file"). Here's the code I used to upload the file to the folder on the server:




$file = ($_FILES['uploaded']['name']);

/*upload*/
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{



The other main task was to add the data to the database:




$name = $_REQUEST['name'] ;
$tags = $_REQUEST['tags'] ;
$submittedby = $_REQUEST['submittedby'] ;
$file = ($_FILES['uploaded']['name']);
$level=serialize($_POST['level']);
$type=serialize($_POST['type']);


mysql_query("INSERT INTO Resources
(name, tags, level, type, url, submittedby) VALUES(' $name ', ' $tags ', ' $level ', ' $type ', '$file', '$submittedby') ")

or die(mysql_error());

echo "Data Inserted!";
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}

if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}




... Just don't forget to connect to mySQL first.

What I've Been Up To

Hi All,
I was pretty ill for the last couple of weeks so missed the lectures, but I've been working hard on my site and getting it operating. I thought I would share with you some of what I've been doing.

Javascript form validation

I've learnt how to validate forms by checking if a field is empty...



if (form.name.value == "") {
alert( "Please enter the name of the resources." );
form.name.focus();
return false ;
}



(For this you need to specify the name of the field you want to validate (name) and if necessary, the id of the form (in this case 'form'))

... and check two passwords are the same



if (form.password.value != form.retypePassword.value)
{
alert("The two passwords are not the same.");
form.retypePassword.focus();
return false;
}



(This specifies the two password fields- 'password' and 'retypePassword')

To make these validations happen, you need to put them inside a function (in this case 'checkform') and add to the form an 'onsubmit' that executes the fuction, ie.



onsubmit="return checkform(this)