Jump to content

Recommended Posts

Hi guys,

I posted a syntax error I was having a problem with a few hours ago with my fopen script, which was solved, however it turns out the whole script has an overall issue with it.

 

<?php
$gallery_name = $_POST['galleryname'];
$path="../gallery/$gallery_name";
mkdir("$path", 0777);

//Create the gallery php file
$ourFileName = "$path/$gallery_name.php";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);

include "loginscript.php";
$sql = mysql_query("INSERT into images (gallery_name) VALUES ('$gallery_name')");

//Write the upload form script to the gallery php file
$myFile = "$path/$gallery_name.php";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Welcome to '$gallery_name'\n <br />";
$stringData.="<?php\n";
$stringData.="include('../../storescripts/connect_to_mysql.php');\n";
$stringData.="\$gallery=\"$gallery_name\";\n";
$stringData.="\$query=mysql_query(\"SELECT image_path FROM images WHERE gallery_name='\$gallery'\");\n";
$stringData.=" if(@mysql_num_rows($query) > 0){\n";
$stringData.="\$image_path=\$row['image_path'];";
$stringData.="echo \"<table>\";\n";
$stringData.="\$count = 0;\n";
$stringData.="while(\$row = mysql_fetch_array(\$query)){\n";
$stringData.="if(\$count == 0){\n";
$stringData.="echo \"<tr><td>\";\n";
$stringData.="}else {\n";
$stringData.="echo \"<td>\";\n";
$stringData.="}\n";
$stringData.='echo "<img src=\'$image_path\' />";';
$stringData.="if (\$count == 3){\n";
$stringData.="echo \"</td></tr>\";\n";
$stringData.="\$count = 0;\n";
$stringData.="}else {\n";
$stringData.="echo \"<td>\";\n";
$stringData.="\$count++;\n";
$stringData.="}\n";
$stringData.="}\n";
$stringData.="\$cells_left = 4 - \$count;\n";
$stringData.="if( \$cells_left > 0){\n";
$stringData.="\$i = 0;\n";
$stringData.="while (\$i <= \$cells_left){\n";
$stringData.="echo \"<td></td>\";\n";
$stringData.="\$i++;\n";
$stringData.="}\n";
$stringData.="echo \"</tr>\";\n";
$stringData.="\n}";
$stringData.="\n";
$stringData.="echo \"</table>\";\n";
$stringData.="} else {\n";
$stringData.="echo \"No Images in the Gallery\";\n";
$stringData.="}\n";
$stringData.="?>\n";
fwrite($fh, $stringData);
fclose($fh);
?>

<?php echo '<a href="managegallery.php">Go Back</a>'; ?>

 

That is my code which writes a script to a new file to take all the images from a directory and display them in a dynamic table. The table has 4 fixed columns and then a variable number of rows based on the number of images.

 

 

The issue I am having is once this code writes the gallery page, this page then doesn't display images. I can edit the gallery page so that it displays images but then if I make these changes to the fwrite script I get syntax errors that I am unable to solve. I'm stuck between having a valid fwrite script and display errors or display images with fwrite errors.

 

Sorry, if I've made it sound confusing.

 

Any help would be gratefully appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/244505-fwrite-problems/
Share on other sites

You shouldn't be creating and writing .php pages to do this.

 

What you should be doing is have 1 (one) gallery.php page that accepts the gallery name as a get parameter on the end of the url when it is requested. The code in gallery.php (or whatever name you choose to call it) would then query the database using the gallery name $_GET parameter it was passed and output the images that are associated with that gallery name.

 

Edit: Programming languages have variables so that you can write a (one) program that operates on different data by simply setting up a variable that the code uses to determine what data it should operate on.

Link to comment
https://forums.phpfreaks.com/topic/244505-fwrite-problems/#findComment-1255903
Share on other sites

Something like this (I guessed at your paths, so they would need to be adjusted to match your actual configuration) -

<?php
include($_SERVER['DOCUMENT_ROOT'] . '/storescripts/connect_to_mysql.php'); // database connection

$image_path = "/storescripts/gallery/"; // domain relative path (the leading slash) to where the image galleries are at
$title_content = "Gallery"; // default page title (if not replaced by an actual gallery name in the following code)
$num_col = 4; // number of columns for the image gallery

// Produce a navigation menu (of some kind) - get and display a list of galleries
$query = "SELECT gallery_name FROM images GROUP BY gallery_name";
if(!$result = mysql_query($query)){
// query failed due to an error. Handle that error here...
trigger_error("Query failed: $query, Error: " . mysql_error());
} else {
$menu_content = '';
if(!mysql_num_rows($result)){
	// no rows found
	$menu_content .= "There are no galleries";
} else {
	// at least one row found
	$menu_content .= "Select a gallery -<br />";
	$menu_content .= "<ol>\n";
	while($row=mysql_fetch_assoc($result)){
		$menu_content .= "<li><a href='?gallery_name={$row['gallery_name']}'>{$row['gallery_name']}</a></li>\n";
	}
	$menu_content .= "</ol>\n";
}
}

// Produce the requested gallery
$gallery_name = isset($_GET['gallery_name']) ? $_GET['gallery_name'] : '';
$gallery_content = '';
if(empty($gallery_name)){
$gallery_content .= "No gallery is selected";
} else {
$query = sprintf("SELECT image_path FROM images WHERE gallery_name='%s'",
	mysql_real_escape_string($gallery_name));
if(!$result = mysql_query($query)){
	// query failed due to an error. Handle that error here...
	trigger_error("Query failed: $query, Error: " . mysql_error());
} else {
	// query succeeded
	if(!mysql_num_rows($result)){
		// no matching rows
		$gallery_content .= "No Images in the Gallery";
	} else {
		// at lest one matching row
		$title_content = $gallery_name; // set gallery name as the page title
		$gallery_content .= "Welcome to $gallery_name<br />\n";
		$gallery_content .= "<table>\n";
		$image_path .= "$gallery_name/"; // produce the actual path - /domain relative path/gallery name/
		$count = 0;
		while($row=mysql_fetch_assoc($result)){
			if($count == 0){
				// start a new row
				$gallery_content .= "<tr>";
			}
			// output each td cell
			$gallery_content .= "<td><img src='$image_path{$row['image_path']}' alt=''></td>";
			$count++;
			if($count == $num_col){
				// end previous row
				$gallery_content .= "</tr>\n";
				$count = 0; // reset count
			}
		}
		if($count !=0){
			// complete any partial row
			while($count < $num_col){
				$gallery_content .= "<td></td>";
				$count++;
			}
			$gallery_content .= "</tr>\n";
		}
		$gallery_content .= "</table>\n";
	}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $title_content; ?></title>
<style type="text/css">
</style>
</head>
<body>
<div>
<?php echo isset($menu_content) ? $menu_content : ''; ?>
</div>
<div>
<?php echo isset($gallery_content) ? $gallery_content : ''; ?>
</div>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/244505-fwrite-problems/#findComment-1256199
Share on other sites

Thank you!!! I had written a very simple $_GET code but wasn't quite sure if it would work, but your's is right on the money! Thanks also for the comments, I learnt a bit about php just from those alone :)

 

Now just to hit up google and find out about multiple image uploads.

 

Link to comment
https://forums.phpfreaks.com/topic/244505-fwrite-problems/#findComment-1256216
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.