how to use every item in the loop - php

I'm created a new script with php. I manipulate the picture uploaded by the user with 40 different models and show them in the page.
The user has to wait until all 40 pictures are prepared with my current code.
$file = $this->post['uploaded_image'];
$models = $this->post['models'];
/*
User selected models, etc:
['model-1', 'model-2', 'model-8', 'model-14', 'model-22', 'model-34', 'model-35', 'model-40']
*/
foreach ($models as $model) {
$resize_image = $this->resize($file, $model);
$final_image = $this->crop($resize_image, $model, $file);
return $final_image;
}
I want to do is to use each image that return from the loop in order. While I use the first image and the loop continues to render in the 2nd, 3rd...
I hope I explained my problem correctly. How can I solve this problem with php or javascript?

Related

OctoberCMS get list of content files

Trying to get content files started with cont*
using :
Content::loadCached('theme', 'listOfContentFiles');
And getting an error.
I can get one but not the list.
Seems there is no direct way of doing it, you can use this code to get list manually and filter it by your self
use Cms\Classes\Content;
use Cms\Classes\Theme;
$activeTheme = Theme::getActiveTheme();
$instance = Content::inTheme($activeTheme);
$items = $instance->newQuery()->lists('fileName');
$loadedItems = [];
foreach ($items as $item) {
// we need to manually filter data you can
// add more logic here for sub directory parsing etc
if(starts_with($item, 'cont_')) {
$loadedItems[] = Content::loadCached($activeTheme, $item);
}
}
dd($loadedItems);
// if you want to make it collection
$result = $instance->newCollection($loadedItems);
it will return you list of content files in active theme by our filter logic.

Laravel 5 relationsheep between article and images

Hi i'm new in Laravel and i've this simple problem. I have app where user can create some article and also can to upload main image for that article and it works perfect. I also want to give possibility to user to upload more images for his article. I already create model (ArticleImage) and add relationship between Article and ArticleImage. And i created form field for uploading multiple file inside the article form. But i don't know now how to tell laravel to save all names of these paths into database and store image inside the public/images folder, than later in views i can use these images. Do i need now to create ArticleImage controller and inside controller to write that function or i should do that inside Article controller inside store function. Tnx in advance for every help.
Here is link to github-repo(https://github.com/Dabizlja/Laravel-blog)
If form sends input as array.
<input name="picures[]" />
In your ArticleController on save you can use a json type
$files=[];
$pictures = $request['pictures'];
if (count($pictures) > 0 && $pictures[0] != null) {
foreach ($pictures as $picture) {
$validator = Validator::make([$picture], ['mimes:jpeg,jpg,bmp,png']);
if ($validator->fails()) {
return $validator->messages()->all();
}
$picture->move(public_path() . '/images/', $picture->getClientOriginalName());
array_push($files, $picture->getClientOriginalName());
}
}
$article->images = json_encode($files);
To get it back in the view
<?php
foreach ( $images as $file) {
echo '<img src="'asset('/images/' . $file)'"/>';
}
?>
In your controller on edit remember to decode the json
$files = json_decode($article->images);
To delete a picture you would have to go through the array and remove it the same way.
Hope this works for you.
May be
$article->articleImages()->saveMany($images);
Hopefully this will solve your problem

get ids of uploads and pass to controller, codeigniter

Does anyone know how I can get the just uploaded images ids and access the array of them from my controller?
My model: http://pastebin.com/aJW0vq22 (do_upload())
And here's the relevant part of my controller:
class Site extends CI_Controller {
function index() {
//enable profiler
//$this->output->enable_profiler(TRUE);
$data = array();
$this->load->model('Site_model');
if($this->input->post('upload')) {
$data['upload'] = $this->Site_model->do_upload();
//echo '<hr />' . $this->Site_model->total_uploads;
//set the users edit session for their image
$uploaded_image_id = $this->Site_model->get_last();
$values = array(
'image_id' => $uploaded_image_id,
'session_id' => $this->session->set_userdata('session_id')
);
$this->session->set_userdata('edit', $values);
//var_dump($values);
//show uploaded image
redirect($uploaded_image_id . '?links');
}
Currently I've just been using 'get_last()' to just get the last thing added to the database, but now I've added the ability to upload multiple things at once I doubt I can still, any ideas?
edit:
basically the end result i'm trying to get is
redirect('id1, id2, id3' . '?links');
You could save the file names of the just uploaded images into a session array and then query your database for the ids of those file names. This will reliably return the desierd ids.
I see that for your get_last you use a query that returns the last image added to the table. But what if you have 500 users uploading an image at the same time?

How to count the number of user downloads

This code for extract files dir and title. The user download when clicked the file link. I need to count then number of downloads. How to do this with cakephp or php?
downloads_controller.php:
function index() {
$this->set('downloads', $this->paginate());
}
downloads/index.ctp:
<?php
$title = $download['Download']['title'];
// output filetitle
$filename = '/files/'.$download['Download']['filename'];
// output http://localhost/tet/files/un5titled0.rar
echo $this->Html->link($title, $filename,array('escape' => false));
?>
not this way i am afraid.
you either need to redirect from an action (which counts before redirecting) or use Media view to pass it through.
thats how I do it.
In the action you can then raise the count prior to sending the file.
If you want to count downloads, you should create a function that serves those downloads and create a field in your database that increments downloads each time this function is called.. For example
Call the following function from your view passing the $filename and the $id
To try out at first use, taking ID=4 as one of the downloads ID in your DB
http://www.yourdomain.com/downloads/download/4
And Then your controller would be...
Class DownloadsController extends AppController{
// All your other functions here
function download($id = null){
$this->Article->updateAll(
array('Download.count' => 'Download.count+1'),
array('Download.id' => $id)
);
$download = $this->Download->findById($id);
$this->set('filename', $download['Download']['filename']);
//$filename is an array that can then be used in your view to pass the file for download. to find out what is has you can use debug($filename) in your view
}
}
Then you need to create a special layout so the browser knows that the request file is a download and you will also need to create a view for download.ctp. Basically when you click the file link on your page, it will call this function which will use its view to serve the file.
You can access the following which will provide some insight on what needs to be done..
TUTORIAL LINK
There are lots of techniques, though in the simplest way, you can use a text file to do this.
Create a txt file and write 0 (zero) into it.
In your index function, read the content of your file.
$counter = file_read_contents('counter.txt');
Increase the read value by 1.
$counter++;
Write new value into file again.
file_put_contents('counter.txt', $counter);
So, it counts downloads and keep number in that file.
function download($id = null) {
$download = $this->Download->findById($id);
$this->set(compact('download'));
if(!empty($id)) {
// increment the number of items downloads
$this->Download->save(array(
'Download' => array(
'id' => $id,
'counts' => ($download['Download']['counts'] + 1)
)
));
}
header('Location: http://localhost/tet/files/'.$download['Download']['filename']);
exit();
}

Query string, populating a page from DB or file

Is this facebook link populated fully from the DB? Or, is it a physical file with PHP in it? Just, how is this page called?
http://www.facebook.com/profile.php?id=49300915&sk=photos
They probably do something like:
if(isset($_GET['id'], $_GET['sk'])) {
mysql_query("SELECT info, photos FROM users WHERE id = '$id'");
}
I'm trying to ask, how do they include this page? Is it like Drupal / any CMS where the PHP and page is stored in the DB, or is it a physical file on the server? If the latter, what's the best way to get the file (case insensitive URL)?
I would have a class with a single method, which reads 'sk' and runs another method, depending on what it's value is.
One method would be 'photos' which would read 'id' and fetch a photo from the database. It would then run another method, displayPage, which will display a page from that data.
The displayPage method takes a "template" filename and an array of variables to provide to the template. It sets up a smarty object, provides the variables, and instructs it to display the template.
Inside the template, I'd include another template for the global header that's on every page in the site, then i'd have the html page content, using smarty to insert dynamic values, then include a global footer.
Note that i've simplified this system a lot. A real page like that would take me a week to write all the code, since a big website does a lot of stuff just to display a simple page (for example: find out if the logged in user actually has access to the page... i don't have access to the example one you gave).
<?php
// profile.php
class ProfileController
{
public function run()
{
if ($_GET['sk'] == 'photos')
return $this->photosPage();
}
protected function photosPage()
{
$id = (int)$_GET['id'];
$result = mysql_query("select * from photo where id = $id");
$photo = mysql_fetch_object($photo);
$this->displayPage('view-photo.tpl', array('photo' => $photo);
}
protected function displayPage($templateFile, $templateVariables)
{
$smarty = new Smarty();
foreach ($templateVariables as $variableName => $variableValue) {
$smarty->assign($variableName, $variableValue);
}
$smarty->display($templateFile);
}
}
$conntroller = new ProfileController();
$controller->run();
And the smarty code:
<!-- view-photo.tpl -->
{include file='head.tpl'}
<h1>View Photo {$photo->name|escape}</h1>
<img src="{$photo->src|escape}" width="{$photo->width|escape} height="{$photo->height|escape}>
{include file='foot.tpl'}

Categories