Link to home
Start Free TrialLog in
Avatar of ST3VO
ST3VOFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Assistance needed with code

Hi all,

I have some code which I need modifying.
Thee code works great but I need to assign a specified location (saving url) to each record (see the array in the code)

The code works great but it only save's the file to 1 location. I need to assign a location per record (hard coded).

Let me know if you need any further explanation and hope you can help.

Also, in the current array there are currently 4 images showing across and I need to add more but for every 4 images I add I need No 5 to 8 to be on another line (e.g <br>  if count > 4 )

Hope you can help.

thx

st3vo

Full Code attached below:

<?php
 
// Check for posted data
if(isset($_POST['copy']) and isset($_POST['copyto']) and $_POST['copyto'] != '')
{
        
	
		if(copy($_POST['copy'], "{$_POST['copyto']}"))
               {
                echo "<p>Template copied OK. <a href=".">Click here to return</a>.</p>";
				//echo $_POST['copyto'];
				$newname = $_POST['copyto'];
				echo "This new name is :".$newname;
              }
        else
        {
                echo "<p>Copy failed. <a href=".">Click here to return</a>.</p>";
        }
}
else
{
        // Get the list of templates (you could generate this from a listing of the directory)
        $templates = array(
                'template1.php' => 'tmb/image1.jpg',
                'template2.php' => 'tmb/image2.jpg',
                'template3.php' => 'tmb/image3.jpg',
                'template4.php' => 'tmb/image4.jpg'
				
        );
		
		?>
        
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>some title</title>
 
<style type="text/css">
.imageOff{
border: 0px;
}
.imageOn{
border: 5px double #ccc;
} 
 
body {
	font-family:Arial, Helvetica, sans-serif;
	font-size:13px;
	font-weight:bold;
}
</style>
</head>
 
<body>
 
<?php
        // Display form
		echo "<center>";
        echo '<form method="post">';
		echo '<p>Enter the name for the new page'; 
		echo '<input name="'.copyto.'" value="NewPageName.php">';
		echo '</p><p>then click a template.</p>';
		
		echo '<br>';
		
        
		foreach($templates as $template=>$thumbnail)
		
		
        {
             
				
				echo '<input class="imageOff" type="image" src="'.$thumbnail.'" onmouseover="this.className=\'imageOn\'" onmouseout="this.className=\'imageOff\'" name="copy" value="'.$template.'">';
				
        }
        echo "\n</form></center>";
	
}
?>
 
</body></html>

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Can you please rephrase the question - it may be that the "copy" and "copyto" values are unclear?
Sorry - I have to leave now.  Here is the script in a slightly different form using a radio to select templates and with correct code indenting.  Look it over and post back here if you have questions or need further assistance.

best, ~Ray
<?php // RAY_temp_st3vo.php
error_reporting(E_ALL);
 
// DEFAULT VALUES FOR FORM INPUT
$from = '';
$copy = '';
 
// LIST OF TEMPLATES
$templates = array(
    'template1.php' => 'tmb/image1.jpg',
    'template2.php' => 'tmb/image2.jpg',
    'template3.php' => 'tmb/image3.jpg',
    'template4.php' => 'tmb/image4.jpg',
    'template5.php' => 'tmb/image5.jpg',
    'template6.php' => 'tmb/image6.jpg'
);
 
// IF ANYTHING POSTED
if (!empty($_POST))
{
// ADD APPROPRIATE EDITING CODE HERE TO CHECK THE INPUT
    if (!empty($_POST["from"])) $from = $_POST["from"];
    if (!empty($_POST["copy"])) $copy = $_POST["copy"];
 
// COPY THE from TO THE copy
    if (copy($from, $copy))
    {
        echo "<p>Template copied to $copy</p>";
    } else
    {
        echo "<p>Copy failed.</p>";
    }
 
// SHOW THE POST DATA
    echo "<pre>\n";
    var_dump($_POST);
    die('PROCESSING COMPLETE');
}
?>
 
 
 
<form method="post">
Enter the name for the new page:
<input name="copy" value="NewPageName.php">
<br/>then choose the template:
 
<?php
// ITERATE OVER THE TEMPLATES
$kount = 0;
foreach($templates as $template => $thumbnail)
{
    $kount++;
    if ($kount > 4)
    {
        echo "<br/>\n";
        $kount = 0;
    }
 
    echo "<br/><input type=\"radio\" name=\"from\" value=\"$template\" /> $thumbnail \n";
}
?>
<br/>
<input type="submit" />
</form>

Open in new window

Avatar of ST3VO

ASKER

Sure...

I basically have Templates...

 $templates = array(
                'template1.php' => 'tmb/image1.jpg',
                'template2.php' => 'tmb/image2.jpg',
                'template3.php' => 'tmb/image3.jpg',
                'template4.php' => 'tmb/image4.jpg'

On the page images appear....

If I click on image1 for example...the code will make a copy of template1.php and save it to the current directory under the new given name which is posted  via input box.

Upto there is all works fine but my problem is this:

eg 'template1.php' => 'tmb/image1.jpg',

Above I have template1.php and image1.jpg - so when you click on image1 a copy of template1.php is made and save under the given name to the specified directory assigned here:

 if(copy($_POST['copy'], "{$_POST['copyto']}"))  can be changed to:

 if(copy($_POST['copy'], "{../ $_POST['copyto']}")) for example.

So I need to assigned a different path to save to for each template (not posted from the form...but hardcoded)

Hope this helps

thx







Avatar of ST3VO

ASKER

Just 1 question...

Where do I assign the path's where the files are save to?

thx

I have added this solution to your old question. Here it is again.

Olly.
<?php
$templates = array(
	'temp1' => array('thumbnail' => 'tmb/1.png', 'path' => '../somedir1'),
	'temp2' => array('thumbnail' => 'tmb/2.png', 'path' => '../somedir/sub'),
	'temp3' => array('thumbnail' => 'tmb/3.png', 'path' => '../anotherdir'),
	'temp4' => array('thumbnail' => 'tmb/4.png', 'path' => '../'));
// Check for posted data
if(isset($_POST['copy']) and isset($_POST['copyto']) and $_POST['copyto'] != '')
{
	if(copy($_POST['copy'], $templates[$_POST['copy']]['path'].'/'.$_POST['copyto'].'.htm')) // Add the directory component to the destination path
	{
		echo "<p>Template copied OK. <a href=".">Click here to return</a>.</p>";
	}
	else
	{
		echo "<p>Copy failed. <a href=".">Click here to return</a>.</p>";
	}
}
else
{
	// Display form
	echo "\n<form method=\"post\">\n<p>Enter the name for the new page <input name=\"copyto\" value=\"newpage\">.htm</p>\n<p>then click a template.</p>";
	foreach($templates as $templatename => $template)
	{
		echo "\n\t<input type=\"image\" src=\"{$template['thumbnail']}\" name=\"copy\" value=\"{$templatename}\">";
	}
	echo "\n</form>";
}
?>

Open in new window

Avatar of ST3VO

ASKER

Perfect ollyatstithians,

Just one thing I need modified pls.

Currently there are 4 images per row and I need to add another 4 (on the next row) how do I do that please?
Just add extra elements to the array to get more layouts:
$templates = array(
        'temp1' => array('thumbnail' => 'tmb/1.png', 'path' => '../somedir1'),
        'temp2' => array('thumbnail' => 'tmb/2.png', 'path' => '../somedir/sub'),
        'temp3' => array('thumbnail' => 'tmb/3.png', 'path' => '../anotherdir'),
        'temp4' => array('thumbnail' => 'tmb/4.png', 'path' => '../'),
        'temp5' => array('thumbnail' => 'tmb/5.png', 'path' => '../somedir'),
        'temp6' => array('thumbnail' => 'tmb/6.png', 'path' => '../anotherdir'),
        'temp7' => array('thumbnail' => 'tmb/7.png', 'path' => '../'));

To arrange the images into rows, you need to either use a table or CSS.

CSS is usually a better way to do it...

Line 25:
  echo "\n\t<input style=\"float:left; margin: 1em;\" type=\"image\" src=\"{$template['thumbnail']}\" name=\"copy\" value=\"{$templatename}\">";

But if you really want a table...
<?php
$templates = array(
	'temp1' => array('thumbnail' => 'tmb/1.png', 'path' => '../somedir1'),
	'temp2' => array('thumbnail' => 'tmb/2.png', 'path' => '../somedir/sub'),
	'temp3' => array('thumbnail' => 'tmb/3.png', 'path' => '../anotherdir'),
	'temp4' => array('thumbnail' => 'tmb/3.png', 'path' => '../anotherdir'),
	'temp5' => array('thumbnail' => 'tmb/3.png', 'path' => '../anotherdir'),
	'temp6' => array('thumbnail' => 'tmb/3.png', 'path' => '../anotherdir'),
	'temp7' => array('thumbnail' => 'tmb/3.png', 'path' => '../anotherdir'),
	'temp8' => array('thumbnail' => 'tmb/4.png', 'path' => '../'));
// Check for posted data
if(isset($_POST['copy']) and isset($_POST['copyto']) and $_POST['copyto'] != '')
{
	if(copy($_POST['copy'], $templates[$_POST['copy']]['path'].'/'.$_POST['copyto'].'.htm')) // Add the directory component to the destination path
	{
		echo "<p>Template copied OK. <a href=".">Click here to return</a>.</p>";
	}
	else
	{
		echo "<p>Copy failed. <a href=".">Click here to return</a>.</p>";
	}
}
else
{
	// Display form
	echo "\n<form method=\"post\">\n<p>Enter the name for the new page <input name=\"copyto\" value=\"newpage\">.htm</p>\n<p>then click a template.</p>\n<table>\n\t<tr>";
	$cols = 4; // Change this to the number of columns you want
	$i = $cols;
	foreach($templates as $templatename => $template)
	{
		if(--$i < 0)
		{
			$i = $cols;
			echo "</tr>\n\t<tr>";
		}
		echo "\n\t\t<td><input type=\"image\" src=\"{$template['thumbnail']}\" name=\"copy\" value=\"{$templatename}\"></td>";
	}
	echo "\n\t</tr>\n</table>\n</form>";
}
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ollyatstithians
ollyatstithians
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ST3VO

ASKER

Perfect thanks :o)