Jump to content

php array result to string question


htakeuchi

Recommended Posts

Hello:

 

I would like to request an opinion so maybe I can find the reason which I am not being able to see for the following.

 

I am using an array that will organize the content of a directory in the oldest to newest order, once it is ordered, display it's content.

 

So far, so good, where I find my self stumped is when I want to send the array result to a csv file for record keeping. I am getting the word "Array" instead of the file name,... What am I doing wrong?

 

I hope someone can assist me in understanding what am I doing wrong.

 

 

Thanks...

 

Hiroshi T

 

 

 

<?php

$files = glob( './images/*.*' );

array_multisort(

array_map( 'filectime', $files ),

SORT_NUMERIC,

SORT_ASC,

$files

);

echo '<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<link href="styles.css" rel="stylesheet" type="text/css" />

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Test Site</title>

</head>

<body>

<div id="container">

<div id="formcontainer">

<form method="POST">

Picture Count: <input type="text" name="pcount"><br>

<input type="submit" value="Submit">

</div> <!-- End of form Container-->

<div id="imgcontainer">

<div id="imgframe">

<iframe src="'.$files[0].'" width="775" height="580" scrolling="no" frameborder="0">

</div> <!-- End of Image frame -->

</div> <!-- End of Image Container -->

</div> <!-- End of Container ID -->

</body>

</html>';

//$count = _POST["pcount"];

$imageDB = "imageDB.csv";

$DateStamp = date("m-d-Y");

$TimeStamp = date("H:i.s");

$openDB = fopen($imageDB, 'a') or die("Cannot update Database!");

$Data2appnd = "$files,$count,$DateStamp,$TimeStamp.\n";

fwrite($openDB,$Data2appnd);

fclose($opendDB);

 

?>

Link to comment
Share on other sites

"Array" is what is output when attempting to output an array directly, which is what $files is.

To maintain the CSV structure, perhaps implodeing the $files array using a particular character would be the best solution so that you can explode it upon reading the file:

 

$filesStr = implode("-", $files);

 

You could also insert a new line per $files element or store the data inside a database, depends on your logic

Edited by AyKay47
Link to comment
Share on other sites

Thank you for both answers...

 

The goal is to have a directory that is being populated by an FTP service, where images are being deposited, with this small php code, the goal is to open each image starting from the oldest inside the directory, once this is shown, using a form, the individual verifying the image, will set a comment via the form _GET or _POST. Once this is submitted, the image is moved to a different directory, (This part has not been written yet)

 

The part where I was asking for help, needs to record the opened file's name and the comment form the form in a csv file, which also records date and time stamps.

 

I believe "Barand's" option would be the best... I will get right to it and try both options and will report back... I sincerely appreciate your expertise.

 

Kind regards

 

 

Hiroshi T

Link to comment
Share on other sites

barands post was for information gathering purposes not a solution.

If I understand your logic correctly, you first need to solve the problem of iterating through all of the photos in the directory.

Perhaps something like this in between the <form> tags:

 

//loop through files
foreach( $files as $image )
{
 //generate html input elements for each image
 echo "<input type='hidden' name='image[]' value='$image' />";
 echo "<input type='text' name='count[]' />";
}

 

Then when validating form input:

 

//perform any scrubbing necessary on user input
$image = $_POST['image'];
$count = $_POST['count'];
//initialize other necessary variables
$dateStamp = date("m-d-y");
$timeStamp = date("H:i.s");
$DBHandle = fopen("imageDB.csv", "a");
if($DBHandle)
{
 //iterate through $_POST values from form and insert into csv file
 foreach( $image as $key => $img )
 {
   $data = "$img,$count[$key],$dateStamp,$timeStamp\n";
   fwrite($DBHandle, $data);
 }
}

 

This will display all of the images and corresponding text fields in the same form. When the user submits the form the script will store the necessary data into the csv file.

Keep in mind that this is only a skeleton to get you on the right path.

Edited by AyKay47
Link to comment
Share on other sites

Thank you very much!

 

AyKay47, thank you very much for your reply, you are correct. Although, for some strange reason and very "Stupid" on my part, What I needed was to record on the CSV is the name of the oldest file being displayed (in this case $files[0]), prior to my posting here, I truly believe I requested that on the fwrite() command, but something must have not been written correctly.

 

Last night I managed to get what I needed by simply adding the $files[0] on the data2append section, which in exchange, writes the file name on the CSV.

 

I am very grateful for your assistance and for taking time to assist me.

 

Here is how the Code looks right now and does what I wanted.

 

Thanks again,

 

Hiroshi T

 

 

---

 

 

<?php
$files = glob( './images/*.*' );
array_multisort(
array_map( 'filectime', $files ),
SORT_NUMERIC,
SORT_ASC,
$files
);
echo '<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="styles.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test Site</title>
</head>
<body>
<div id="container">
<div id="formcontainer">
        <form method="POST">
People Count: <input type="text" name="pcount"><br>
<input type="submit" value="Submit">    
    </div> <!-- End of form Container-->
    <div id="imgcontainer">
        <div id="imgframe">
<iframe src="'.$files[0].'" width="775" height="580" scrolling="no" frameborder="0">
   </div> <!-- End of Image frame -->
    </div> <!-- End of Image Container -->
</div> <!-- End of Container ID -->
</body>
</html>';
$imageDB = "imageDB.csv";
$DateStamp = date("m-d-Y");
$TimeStamp = date("H:i.s");
$openDB = fopen($imageDB, 'a') or die("Cannot update Database!");
$Data2appnd = "$files[0],$count,$DateStamp,$TimeStamp.\n";
fwrite($openDB,$Data2appnd);
fclose($opendDB);
 
?>
Link to comment
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.