This topic is locked

Default image in empty field

12/18/2013 12:38:36 PM
PHPRunner General questions
J
jianwong author

I have this mysql field 'photo' for user photo in table 'users'. The Primary key for the table is 'userid'.
When the 'photo' field is empty, I want a custom image to show.
The field type of 'photo' is mediumtext, the upload folder where user photo is stored named 'users_photos'.
I used the following code in Custom 'View as' setting on Editor.
When field is empty, the custom image (no_image.gif) shows.
But the image file that added to the field won't show.
My knowledge in php is limited. I hope someone can help point out to me where the code went wrong or what is missing.
===========

if ($value)

$value="<img border=0

src=\"mfhandler.php?filename=file.jpg&table=users&field=photo&nodisp=1&pageType=list&key1=".$data["userid"]."\">";

else

$value = "<img border=0 src=\"images/no_image.gif\">";

Admin 12/18/2013

Your code looks correct however it assumes that file name is file.jpg which is not always the case.
What is the actual content of photo field in the database when it does have uploaded file info?

J
jianwong author 12/19/2013



Your code looks correct however it assumes that file name is file.jpg which is not always the case.
What is the actual content of photo field in the database when it does have uploaded file info?


Thanks for the response.
The database type for 'photo' field is mediumtext, on 'Edit as' I selected File/Image, upload folder is 'users_photos', in Custom 'View as' I used the above code.
The file type that I uploaded was jpg file. It won't show the uploaded file, but it shows the 'no_image.gif' if the field is empty.

C
copper21 12/19/2013

I did notice that you are using "$value" in your IF statement. In a "view as" I think you need to use "$data".

Admin 12/19/2013

Jian,
I still need my answer. Here is the question:

What is the actual content of photo field in the database when it does have uploaded file info?

J
jianwong author 12/19/2013



Jian,
I still need my answer. Here is the question:

What is the actual content of photo field in the database when it does have uploaded file info?


Sergey
I hope I got the answer right. This is exactly what I found in the 'user_photo' field where an image is uploaded.
[{"name":"081008_hpjzt8rq.jpg","usrName":"081008.jpg","size":3015,"type":"image\/jpeg","searchStr":"
And below is the code that I have in Custom 'View as' setting on Editor for field 'user_photo'

============

if ($value)

$value="<img border=0

src=\"mfhandler.php?filename=file.jpg&table=users&field=user_photo&nodisp=1&pageType=list&key1=".$data["userid"]."\">";

else

$value = "<img border=0 src=\"images/no_image.gif\">";

===========
I appreciate your help.

Admin 12/20/2013

Thanks, this is what I was asking. Your code is almost right assuming that all table and field names are correct (case-sensitive).
The only missing part is the file name. This sample code uses the hardcoded value of "file.jpg" while you need to read it from the database as well. For instance in your case correct file name will be "081008.jpg".
What you need to do is to extract this file name from the database field value stored in JSON format. You can find examples of parsing JSON format at http://xlinesoft.com/phprunner/docs/rename_uploaded_file.htm

J
jianwong author 12/23/2013



Thanks, this is what I was asking. Your code is almost right assuming that all table and field names are correct (case-sensitive).
The only missing part is the file name. This sample code uses the hardcoded value of "file.jpg" while you need to read it from the database as well. For instance in your case correct file name will be "081008.jpg".
What you need to do is to extract this file name from the database field value stored in JSON format. You can find examples of parsing JSON format at http://xlinesoft.com/phprunner/docs/rename_uploaded_file.htm


Sergey
I'm lost.
I rename the uploaded file using the following code in Add/Edit pages before record added/updated. The image file got renamed, in this case to that of 'contact_name' and file stored in folder named 'userphoto' in the database. But the image file just won't show on page.

=========

// get information about uploaded files

$fileArray = my_json_decode($values["user_photo"]);

// rename files

for($i = 0; $i < count($fileArray); $i++)

{

$fileName = $fileArray[$i]["name"];

$newFileName = "userphoto/".$values["contact_name"].$i.".jpg";

rename($fileName, getabspath($newFileName));

$fileArray[$i]["name"] = $newFileName;

}

// update values of the field that stores file names

$values["user_photo"] = my_json_encode($fileArray);

=========
I think the problem is in my custom "View as" setting on Editor. I can't figure out how to extract the renamed file. I append the custom view code in below hope you could help me on it.

=========

if ($value)

$value="<img border=0

src=\"mfhandler.php?filename=contact_name&table=users&field=user_photo&nodisp=1&pageType=list&key1=".$data["userid"]."\">";

else

$value = "<img border=0 src=\"images/no_image.gif\">";

==========
Thank you.
jian

Admin 12/23/2013

You should not be pasting file renaming code anywhere. This is just an example of how you can parse JSON format and access values of different parameters like "name" or "usrName".
You need to write the code that extrcts the value of usrName parameter from the JSON stored in the database and use this file name in your 'View as' Custom expression. This isn't something you can copy and paste.

J
jianwong author 12/23/2013



You should not be pasting file renaming code anywhere. This is just an example of how you can parse JSON format and access values of different parameters like "name" or "usrName".
You need to write the code that extrcts the value of usrName parameter from the JSON stored in the database and use this file name in your 'View as' Custom expression. This isn't something you can copy and paste.


Can anyone help with a sample code on how I can extract the value of usrName parameter from the JSON? Or a link to some tutorials where I can study and try come out with the code.
Thank you.

Admin 12/23/2013

It is already in the sample code you posted.
The following line from sample code you posted extracts the value of "name" parameter:

$fileName = $fileArray[$i]["name"];


This is the one for "usrName":

$fileName = $fileArray[$i]["usrName"];
J
jianwong author 12/23/2013



It is already in the sample code you posted.
The following line from sample code you posted extracts the value of "name" parameter:

$fileName = $fileArray[$i]["name"];


This is the one for "usrName":

$fileName = $fileArray[$i]["usrName"];



Hi Sergey
I'm almost there. Now I got an error message "rename(081008.jpg,C:\xampp\htdocs\jwdb/userphoto/Jian Wong0.jpg): The system cannot find the file specified. (code: 2)"
How should I correct the custom view as setting in order to point to the image file which is resided in "userphoto" folder? I really appreciate your assistance.
========

if ($value)

$value="<img border=0

src=\"mfhandler.php?filename=file.jpg&table=users&field=user_photo&nodisp=1&pageType=list&key1=".$data["userid"]."\">";

else

$value = "<img border=0 src=\"images/no_image.gif\">";

=======
Merry Christmas to everyone of you in the support team.
jian