Solved

Assistance needed with code

Posted on 2009-05-19
9
211 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:ST3VO
  • 4
  • 3
  • 2
9 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 24427111
Can you please rephrase the question - it may be that the "copy" and "copyto" values are unclear?
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 24427196
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

0
 

Author Comment

by:ST3VO
ID: 24427201
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







0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:ST3VO
ID: 24427339
Just 1 question...

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

thx

0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24429437
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

0
 

Author Comment

by:ST3VO
ID: 24429697
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?
0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24429858
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

0
 
LVL 10

Accepted Solution

by:
ollyatstithians earned 500 total points
ID: 24429872
Or even use a <br> ...

<?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>";
	$cols = 4; // Change this to the number of columns you want
	$i = $cols;
	foreach($templates as $templatename => $template)
	{
		if(--$i < 0)
		{
			$i = $cols;
			echo "<br>";
		}
		echo "\n\t\t<input type=\"image\" src=\"{$template['thumbnail']}\" name=\"copy\" value=\"{$templatename}\">";
	}
	echo "\n</form>";
}
?>

Open in new window

0
 

Author Closing Comment

by:ST3VO
ID: 31583263
Perfect thanks :o)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question