Jump to content

Semi-blog with php


CraigGT

Recommended Posts

Create a test and see.

 

db structure:

create table test (
      id int(2) NOT NULL auto_increment,
      blog text NOT NULL default '',
      PRIMARY KEY (id)
);

INSERT INTO test (id, blog) VALUES (1, 'This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text ');
INSERT INTO test (id, blog) VALUES (2, 'This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text ');
INSERT INTO test (id, blog) VALUES (3, 'This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text ');

 

//blog.txt

1::This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text
2::This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text
3::This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text This is a blog text

 

<?php
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
$id = 2;
$time_start = microtime_float();
mysql_connect('localhost', 'yourusername', 'yourpassword') OR DIE('no connection can be made');
mysql_select_db('yourdbname') OR DIE('no ddb could be selected.');

$query = mysql_query("SELECT id, blog FROM test WHERE id = " . $id . " LIMIT 1");
$row = mysql_fetch_assoc($query);

echo 'The blog with the MySQL id ' . $row['id'] . ' and text . ' $row['text'] . '<br /><br />';
$time_end = microtime_float();
echo 'Took ' $time_end - $time_start . ' seconds to complete!';

$time_start = microtime_float();
$file = file('blog.txt');

foreach ($file as $blogs) {
    list($bid, $blog) = split('::', $blogs);
    if ($bid == $id) {
          echo '<br />The blog with the text id ' . $bid . ' and text . ' $blog . '<br /><br />';
          break;
    }
}

$time_end = microtime_float();
echo 'Took ' $time_end - $time_start . ' seconds to complete!';
?>

 

Let me know which one is faster.

Link to comment
https://forums.phpfreaks.com/topic/53777-semi-blog-with-php/#findComment-265822
Share on other sites

Frost that won't be accurate.

 

Get a text file with 1000 of those entries, and then get a mysql table with 1000 entries.

 

MySQL will beat flat files face in with that test.

 

Yea, but even so, that one should be pretty accurate I would think.

 

Either way if you want to be able to edit the entry etc, MySQL is the way to go. If not than stick with the flat file.

 

Less of a hassle one way or the other.

Link to comment
https://forums.phpfreaks.com/topic/53777-semi-blog-with-php/#findComment-265860
Share on other sites

If we are talking about a lot of images, a database would be more convenient, but for just a few images, or if you don't have access to a database, text files are ok.

 

You could do it like this:

<?php
if (isset($_POST['upload'])) {
if (empty($_FILES['picture']['name'])) {
?>
	<div>
		<p>No file selected. Try again.</p>
	</div>
<?php
} // end no image selected
else {
	$temp_file = $_FILES['picture']['tmp_name'];
	$dest_file = $_FILES['picture']['name'];
	move_uploaded_file($temp_file, "images/$dest_file") or die("Error: Could not copy the uploaded image.");
	// image upload completed, moving on to the description
	$description_file = str_replace(".jpg", ".txt", $dest_file); // setting the name of the textfile to the same as the name of the image, only with .txt ending
	$new_description = fopen("images/$description_file", "w"); // the textfile is located right next to the image
	$descr_content = $_POST['description'];
	fwrite($new_description, $descr_content); // writing the description to the textfile
	fclose($new_description); // close the file
	?>
	<h3>Image uploaded!</h3>
	<p><img src="images/<?php echo $dest_file; ?>" /><br />
		<em>images/<?php echo $dest_file; ?></em></p>
	<p>The associated text is stored in <em>images/<?php echo $dest_file . "_description.txt"; ?></em></p>
	<p>The text:<br /><?php echo $_POST['description']; ?></p>
	<hr />
	<?php
} // end else
} // end if isset($_POST['upload])
?>
<form action="" method="post" enctype="multipart/form-data">
<p><input type="file" name="picture" /></p>
<p><textarea name="description" rows="4" cols="40">Description...</textarea></p>
<p><input type="submit" name="upload" value="Submit" /></p>
</form>

 

And later on retrieve it like this:

<?php
if (isset($_POST['retrieve'])) {
$image = $_POST['image'];
$descr_tmp = str_replace(".jpg", ".txt", $image);
$description = file_get_contents($descr_tmp);
?>
<h3>The image:</h3>
<p><img src="<?php echo $image; ?>"><br /><em><?php echo $image; ?></em></p>
<h4>Description:</h4>
<p><?php echo $description; ?></p>
<hr />
<?php
} // end if retrieve

$folder = "images/";
$folder_ref = opendir($folder);
?>
<h3>Select an image to retrieve</h3>
<?php
while ( $next = readdir($folder_ref) ) {
$filename = "images/" . $next;
if ( strstr($filename, ".jpg")) { // we are only listing .jpg files
?>
<form action="" method="post">
	<p><img height="200" width="200" src="<?php echo $filename; ?>" /><input type="hidden" name="image" value="<?php echo $filename; ?>" /><input type="submit" name="retrieve" value="Fetch" /></p>
</form>
<?php
} // end if .jpg
} // end while
?>

Link to comment
https://forums.phpfreaks.com/topic/53777-semi-blog-with-php/#findComment-265878
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.