Image upload

Image upload

mccloudmccloud Posts: 35Questions: 15Answers: 2
edited February 2017 in Editor

I am attempting to add an image upload to my data-table. I have followed the https://editor.datatables.net/examples/advanced/upload.html, but it only displays the html column headings.
1. I have created a directory uploads on the server with permissions
2. I have added a database field "images" (text).
3. I have edited the js, php and html files.

I have copied the code as shown in the example and appended it to my files.

I also recieve the following error message

DataTables warning: table id=scenario - SQLSTATE[42S02]: Base table or view not found: 1146 Table 'usertest.files' doesn't exist

Am I missing something?

JavaScript

//add upload of image
{
                label: "Image:",
                name: "image",
                type: "upload",
                display: function ( file_id ) {
                    return '<img src="'+editor.file( 'files', file_id ).web_path+'"/>';
                },
                clearText: "Clear",
                noImageText: 'No image'
            }

        ]
    } );

/////
//upload of image
 {
                data: "image",
                render: function ( file_id ) {
                    return file_id ?
                        '<img src="'+editor.file( 'files', file_id ).web_path+'"/>' :
                        null;
                },
                defaultContent: "No image",
                title: "Image"
            }

        ],
        select: true,
        lengthChange: false
    } );

PHP Code

Field::inst( 'image' )
            ->setFormatter( 'Format::ifEmpty', null )
            ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/uploads/__ID__.__EXTN__' )
                ->db( 'files', 'id', array(
                    'filename'    => Upload::DB_FILE_NAME,
                    'filesize'    => Upload::DB_FILE_SIZE,
                    'web_path'    => Upload::DB_WEB_PATH,
                    'system_path' => Upload::DB_SYSTEM_PATH
                ) )
                ->validator( function ( $file ) {
                    return$file['size'] >= 500000 ?
                        "Files must be smaller than 500K" :
                        null;
                } )
                ->allowedExtensions( array( 'png', 'jpg', 'gif' ), "Please upload an image" )
            )
    )
    )

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    but it only displays the html column headings.

    That suggests there is a Javascript error on the page. What does your browser's console show?

    Allan

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    DataTables warning: table id=scenario - SQLSTATE[42S02]: Base table or view not found: 1146 Table 'usertest.files' doesn't exist

    It means that there is not files table, although on line 4 of the above PHP code the files table is referenced. Are you attempting to run the demos? Have you installed the example SQL?

    Allan

  • mccloudmccloud Posts: 35Questions: 15Answers: 2
    edited February 2017

    Thanks Allan. I managed to get it working. The table itself shows the actual image. Is there a way of changing this to jus show the link to the image?

  • mccloudmccloud Posts: 35Questions: 15Answers: 2
    edited February 2017 Answer ✓

    I figured it out. I used an ordinary HTML link instead of the SRC code.

This discussion has been closed.