Exabytes beginner

Showing posts with label Makcik Programmer. Show all posts
Showing posts with label Makcik Programmer. Show all posts

Tuesday, January 6, 2015

PHP MySQL insert date

Answer: 
MySQL recognizes DATE values in these formats:

As a string in either 'YYYY-MM-DD' or 'YY-MM-DD' format. A “relaxed” syntax is permitted: Any punctuation character may be used as the delimiter between date parts. For example, '2012-12-31', '2012/12/31', '2012^12^31', and '2012@12@31' are equivalent.

As a string with no delimiters in either 'YYYYMMDD' or 'YYMMDD' format, provided that the string makes sense as a date. For example, '20070523' and '070523' are interpreted as '2007-05-23', but '071332' is illegal (it has nonsensical month and day parts) and becomes '0000-00-00'.

As a number in either YYYYMMDD or YYMMDD format, provided that the number makes sense as a date. For example, 19830905 and 830905 are interpreted as '1983-09-05'.

Therefore, the string '08/25/2012' is not a valid MySQL date literal. You have four options (in some vague order of preference, without any further information of your requirements):

Configure Datepicker to provide dates in a supported format using its dateFormat option:

$( ".selector" ).datepicker({ dateFormat: "yyyy-mm-dd" });
Use MySQL's STR_TO_DATE() function to convert the string:

INSERT INTO user_date VALUES ('', '$name', STR_TO_DATE('$date', '%m/%d/%Y'))
Convert the string received from jQuery into a PHP timestamp—e.g. using strtotime():

$timestamp = strtotime($_POST['_date']);
and then either:

format the timestamp using date():

$date = date('Y-m-d', $timestamp);
pass the timestamp directly to MySQL using FROM_UNIXTIME():

INSERT INTO user_date VALUES ('', '$name', FROM_UNIXTIME($timestamp))
Manually manipulate the string into a valid literal:

$parts = explode('/', $_POST['date']);
$date = "$parts[2]-$parts[0]-$parts[1]";

Thursday, September 18, 2014

ASP: Passing multidimensional array to another page

While passing single dimension array can be done through string passing, passing the multidimensional array is not that straight forward. A single string cannot not hold multidimensional array element. breaking the array into 2 or more separate strings, though able to pass the value, will not ensure the matching of the array elements to the correct pair.

I assume that many of us know that we can pass array to another asp page using session variable. I would rather find another method than passing any value into a session variable if if i have a choice. Why? Session variable is ASP is equivalent to global variable in visual  basic. You can store anything in session variable. From a simple string to a large ADO object  if you store  a value in session variable and the value is huge it will affect the performance of your website. But are session variables really evil?  You can read more about Pros and cons of session variable.


Method 1: Passing through a Session Variable 

Still , the simplest way to pass the multidimensional array to another page is by using session variable, The codes below show how you can do it.

you have created  and populate a multidimensional array in page 1 and you want to pass it to page 2

Page 1

1. create the array
dim arrtest(3,2)

2. Then you populate your array

arrtest(0,0)="123"
arrtest(0,1)="Hen"
arrtest(0,2)="Chicken"
arrtest(1,0)="123"
arrtest(1,1)="Cat"
arrtest(1,2)="Kitten"

3. Pass it into a session variable. Name is not necessary similar but a meaningful name will make you easy.

session("arrtest")=arrtest

Page 2

1.Extract the array

dim arrtestpass
arrtestpass=session("arrtest")


2. Display the content

for i=0 to ubound(arrtestpass,2)
   
     for j=0 to ubound(arrtestpass,1)
           arrtestpass(i.j)
    next

next


arrtestpass is now an array and can be used in page 2


Method 2: Store in Temporary Database Table

This method is ideal if your array store large data or object such as big size picture.

Page 1

Insert the multidimensional array into a temporary table.The table arrtest_tab need to be created first it it still not exist

CREATE TABLE  arrtest_tab (test_id number animal text, offspring text)

for i=0 to ubound(arrtest,2)
      strinsert='INSERT into arrtest_tab(test_id,animal,offspring) values("
      for j=0 to ubound(arrtest,1)
           strinsert=strinsert & arrtest(i,j)
            if i=ubound(arrtest,2) then strinsert=strinsert & "')" else strinsert=strinsert &"',"
      next
next
if strinsert<>"" then cn.execute(strinsert)

Page 2

Display and delete the temporary record

'if store in database table, query and display
strcart="select item_name, item_qty from acart_tab"
set rscart=cn.execute(strcart)
if not rscart.eof then
acart=rscart.getrows()
end if
To display

for n=0 to ubound(acart,2)
for m=0 to ubound(acart,1)
response.Write acart(m,n)
response.Write "|"
next
response.Write "P"
next

Delete the temporary record from the database

strdelete="TRUNCATE table acart_tab"
cn.execute(strdelete)


Method 3: Chunk  to Separate String by Column

Page 1:

stritem=""
strqty=""
for icart=0 to ubound(acart,2)
for jcart=0 to ubound(acart,1)
if jcart=0 then stritem=stritem & acart(jcart,icart)& ","
if jcart=1 then strqty=strqty & acart(jcart,icart) &","
next
next
stritem=left(stritem,len(stritem)-1)
strqty=left(strqty,len(strqty)-1)
response.redirect "order_checkout.asp?stritem="&stritem&"&strqty="&strqty



Page 2 

'store in multiple string
redim acart(1,0)
aitem=split(request.QueryString("stritem"),",")

aqty=split(request.QueryString("strqty"),",")

for itemcount=0 to ubound(aitem)
redim preserve acart(1,itemcount)
acart(0,itemcount)=aitem(itemcount)
acart(1,itemcount)=aqty(itemcount)
next

To display

for n=0 to ubound(acart,2)
for m=0 to ubound(acart,1)
response.Write acart(m,n)
response.Write "|"
next
response.Write "P"
next


I AM THE AUTHOR
Noraida Arifin
16 years  programming experience and  still learning.
Spent 13 years of my life span as a software engineer
in a multinational company
Currently  a retire programmer.
Feedback are welcome.
Contact me at :
https://www.facebook.com/makcikprogrammer

ASP: Insert Into Database

In progress.... please come again or follow us for update

I AM THE AUTHOR
Noraida Arifin
16 years  programming experience and  still learning.
spent 13 years of my life span as a software engineer
in a multinational company
Currently  a retire programmer.
Feedback are welcome.
Contact me at :
https://www.facebook.com/makcikprogrammer

Wednesday, September 17, 2014

Create MS Access Database table

1.Click on NEW
2. Click on Blank database

3. I name the database CATALOG, Any meaningful name will do.

4. Click on create table in Design view


5. This is example of the field created as example in Classic ASP: Use multidimensional array to keep the shopping cart item before insert into the database
6. I name the table item_tab.


7. Primary key is highly recommended. Even though it is optional, having a primary key.Automatically if you choose YES, the first field in the table will be selected as primary key.
8. Go to VIEW --> Datasheet view. Now we can see that our table has been created in the database.

I AM THE AUTHOR
Noraida Arifin
16 years  programming experience and  still learning.
spent 13 years of my life span as a software engineer
in a multinational company
Currently  a retire programmer.
Feedback are welcome.
Contact me at :
https://www.facebook.com/makcikprogrammer


Tuesday, September 16, 2014

Enabling MSE script Editor for MS Office 2003 and MS Office 2007

MSE Script Editor comes with MS OFFICE 2003 and MS OFFICE 2007 installation. It is easy to use, useful and most importantly free editor any programmer would love.  However it will not be automatically install when we install MS OFFICE . To add the editor we need to:

1.  Run installation file again.
2.  Choose
3. Click next
3.Make sure choose advance customization of applications is CHECKED

4. Click NEXT, then expand Microsoft Office, expand Office Tools, expand HTML Source Editing,

5. Expand  Web Scripting  , find MSE Editor, click on the arrow and  click Run from my computer.

Complete the installation to the end.

Now we need to search for the EXE file. For MS Office 2003 , It is located at:

C:\Program Files\Microsoft Office\OFFICE11\MSE7.EXE

While for MS Office 2007 at:

C:\Program Files\Common Files\microsoft shared\OFFICE12\MSE7.EXE

I AM THE AUTHOR
Noraida Arifin
16 years  programming experience and  still learning.
spent 13 years of my life span as a software engineer
in a multinational company
Currently  a retire programmer.
Feedback are welcome.
Contact me at :
https://www.facebook.com/makcikprogrammer

Monday, September 15, 2014

Classic ASP: Use multidimensional array to keep the shopping cart item before insert into the database

Basically there are to way to store the shopping cart item before the customers checkout and confirm their selection:

1. Store it to database table
2. Store to multidimensional array

In this article, I am going to discus on how to store the shopping cart items into multidimensional array and store the array elements into the database table. To learn how to create MS Access database please view my tutorial; Create MS Access Database table

Traditionally , the shopping catalog will display the items before the user can choose to buy it.

1. First we  need to connect to the database.

' connect to the database 

dim db_string
db_string="Your_database_string"
cs="Driver={Microsoft Access Driver (*.mdb)};"&_
" DBQ="&db_string&";"
  Set cn = server.createobject("ADODB.Connection")
  cn.mode=3  'adModeReadWrite, to avoid unspecified problem error
  cn.open cs

 If you are not sure the path for the database you can ASP server variable to get the virtual path

response.write server.MapPath("catalog.mdb")


Where "catalog.mdb" is your database name.

2. We assume that in catalog.mdb there is a table name item_tab. Item_tab store products details data to be display in the catalog. If you wish to know on  how to input into database table please refer my tutorial on ASP: Insert into Database

This codes will query from the item_tab table and assign to a multidimensional array


' first select records from item_tab and display it

stritem="select item_id,item_name,item_price,item_qty"
stritem=stritem & " from item_tab where item_qty >='1'"
set rsitem=cn.execute(stritem)

if not rsitem.eof then
' assign the recordset into a multidimensional array
aitem=rsitem.getrows()
end if



<form name="frmpick" action="view_cat_test.asp" method=post ID=Form3>

<table>
<%if isarray(aitem) then%>
<td>
<table>
<th>PICK</th>
<th>ITEM ID</th>
<th>ITEM NAME</th>
<th>PRICE</th>
<th>IN-STOCK</th>
<tr></tr>
<td colspan=5 align=center>
<input type="button" name="btnadd" value="ADD TO YOUR CART" onclick="document.frmpick.submit()" ID=Button1>
<INPUT type="hidden" ID=hstrsel name="hstrsel" value="<%=strsel%>">
</td>
</tr>
<% for i=0 to ubound(aitem,2)%>
<tr>
<td><INPUT type="checkbox" ID=Chksel name="chksel" value="<%=aitem(0,i)%>">

</td>
<%for j=0 to ubound(aitem,1)%>
<td><%=aitem(j,i)%></td>
<%next%>
</tr>
  <%next%>
</table>

</td>


The above codes will display the elements of array in HTML table format.

3. Notice the Lines

<td colspan=5 align=center>

<input type="button" name="btnadd" value="ADD TO YOUR CART" onclick="document.frmpick.submit()" ID=Button1&gt


The Button  is used to send the selected item to the shopping cart

4. Once the button is clicked the selected item will be display in the shopping cart.


<!--Display The shopping Cart -->

<td valign="top" align="center">
<table>
<tr>
<th colspan=2>Shopping Cart</th>
</tr>
<% if isarray(acart) then %>
<tr>
<th>Item</th>
<th>Qty</th>
<th>X</th>
</tr>
<%for n=0 to ubound(acart,2)%>
<tr>
<%for m=0 to ubound(acart,1)%>
<td>
<%=acart(m,n)%>

</td>
<%next %>
<td><INPUT type="checkbox" name="chkrem" id="chkrem" value="<%=acart(0,n)%>"></td>
</tr>
<%next%>
<tr>
<td colspan=2>
<INPUT type="submit" value="Remove" ID=Submit1>
</td>
</tr>
<% else %>
<td>
Your shopping cart is currently empty
</td>
<% end if%>
</table>

</td>
</table>
</form>


The background process for "ADD TO CART " button


'Add into the shopping cart

' get the previous select
strsel=trim(request.Form("hstrsel"))
'response.Write "strsel:" & strsel

' 1. Get the checkbox value (current select)
chksel=trim(request.form("chksel"))
chksel=replace(chksel," ","") ' to remove the extra space in the checkbox value
if chksel<>"" then
if strsel="" then
strsel=chksel
else
strsel=strsel & "," & chksel
end if
end if


5. Remove from the shopping cart. Once the item is selected into the shopping cart, it can be remove using this codes:


' remove item from shopping cart 
chkrem=replace(trim(request.form("chkrem"))," ","")
if chkrem<>"" then
'remove the item from array
arem=split(chkrem,",")
if isarray(arem) then
strsel=strsel&","
for p=0 to ubound(arem)
strsel=replace(strsel,arem(p)&",","")
next
end if
if strsel<>"" then strsel=left(strsel,(len(strsel)-1))
end if 

6. Everytime the user click the checkbox , the system will treat the quantity as 1. It will keep on added to the quantity it the item is clicked again. Example of the string created by the process is something similar like:

strsel= "a,a,b,c,a,d,c,e,f"

Technically after assigning to the multidimensional array the array will be;

Item                Quantity
a                           1
a                           1
b                           1
c                           1
a                           1
d                           1
c                           1
e                           1
f                           1

However, the desired  result to be display in the shopping cart should be

Item                Quantity
a                           3
b                           1
c                           2
d                           1
e                           1
f                           1

I use the dictionary scripting object to handle the repetitive array element and do the counting of the quantity:


if strsel<>"" then  

asel=split(strsel,",")
redim acart(2,0)
Set oDic = CreateObject("Scripting.Dictionary")
For k = 0 To UBound(asel)
If oDic.Exists(asel(k)) Then
oDic.Item(asel(k)) = oDic.Item(asel(k)) +1
Else
oDic.Add asel(k), 1
End If
Next
ecount=0
For Each strDate In oDic.Keys()
'response.Write  "<br>item " & strDate & " --" & oDic.Item(strDate) & " qty"
redim preserve acart(2,ecount)
acart(0,ecount)=strDate
acart(1,ecount)=oDic.Item(strDate)
ecount=ecount+1
Next
end if

To view the live example please click :
Code live example


I AM THE AUTHOR
Noraida Arifin
16 years  programming experience and  still learning.
spent 13 years of my life span as a software engineer
in a multinational company
Currently  a retire programmer.
Feedback are welcome.
Contact me at :
https://www.facebook.com/makcikprogrammer

Friday, August 15, 2014

Add facebook comment box to blogger

Hi All,
it has been ages since the last time i talk about 'techie thingy'.. :-) Being a full time mother for now almost 6 months,  my technical section of the brain does not really  functioning to the fullest. The most technical thing that i did .....could be how to sharpen the kitchen knife?

I am going to share the simple steps to include the facebook comment box to our blogger post. What you need to do:

STEP 1: Facebook Application Creation

1. Create facebook application. Go to https://developers.facebook.com/apps .If this is your first app, before you can proceed with the creation, there are some verification needed. The codes will be send to your mobile phone  to activate the developer mode.


Click on the "+Create New App" button.The green button on the top right of the screen.


Go to Setting --> Basic, key in your valid email and save  changes.

You have successfully created you application. Change the application setting to be viewable by others .
 go to status & review. Click on the "NO" button  as below


Your application now is accessible to others.
go to setting--> Basic and note the application Id. this ID will be the ID to be use in the blog setting.

Step 2 : Blogger Configuration


1. Go to your blog, make sure you login to your blogger account. Click DESIGN on the TOP RIGHT of the page.

2. Click on Template --> Edit HMTL

You will get this. For those without coding knowledge , easy.. this will not be as complicated as you thought.


3. Find this line started with

4. Replace html  with html xmlns:fb='http://www.facebook.com/2008/fbml' . Leave the rest of the codes.

yours might be different depend on the setting you have set before. Just make sure the first part in bold is right.

5. Search for the tag
</body>

  Add this code just before the tag.

<div id='fb-root'/>

<script>
window.fbAsyncInit = function() {    FB.init({      appId  : &#39;Your-App-Id&#39;,  
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session    
xfbml  : true  // parse XFBML    });  };  
(function() {    var e = document.createElement(&#39;script&#39;);  
e.src = document.location.protocol +   &#39;//connect.facebook.net/en_US/all.js&#39;;  
e.async = true;  
document.getElementById(&#39;fb-root&#39;).appendChild(e);    }());
</script>


Replace Your-App-Id  with your application ID created in STEP 1

6.  Search for 
</head>

and paste this codes  just before it.
<meta expr:content='data:blog.pageTitle' property='og:title'/> 
<meta expr:content='data:blog.url' property='og:url'/>
<meta content='Journey To Happiness' property='og:site_name'/> 
<meta content='Blog-Logo-Image' property='og:image'/> 
<meta content='Your-App-Id' property='fb:app_id'/> 
<meta content='http://www.facebook.com/Noraida' property='fb:admins'/> 
<meta content='article' property='og:type'/> 
Then Replace:

Journey To Happiness:-                       With Blog Title Name 
Blog-Logo-Image:-                               With Logo Image 
Your-App-Id:-                                        With Facebook App id 
Noraida:-                                               With Facebook Page Username 

7. Now we want to place the Facebook comment box on the blogger page. search for

<b:includable id='comment-form' var='post'> 



paste the below code right after the search line

<b:if cond='data:blog.pageType == &quot;item&quot;'>
<div style='padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;'><script src='http://connect.facebook.net/en_US/all.js#xfbml=1'/>
<div>
<fb:comments colorscheme='light' expr:href='data:post.url' expr:title='data:post.title' expr:xid='data:post.id' width='530'/></div>
</div>
</b:if>


just incase you cannot find <b:includable id='comment-form' var='post'>  try to look for 
 <div class='post-footer'> 

and paste the code above it. If you find it 2 times, paste above the second one. Save the template and view your blog.

Note:

<fb:comments colorscheme='light' expr:href='data:post.url' expr:title='data:post.title' expr:xid='data:post.id' expr:mobile='data:mobile' width='530'/>


 hold the possible setting for your facebook comment box setting



I AM THE AUTHOR
Noraida Arifin
16 years  programming experience and  still learning.
spent 13 years of my life span as a software engineer
in a multinational company
Currently  a retire programmer.
Feedback are welcome.
Contact me at :
https://www.facebook.com/makcikprogrammer

Thursday, August 2, 2007

Sample connection string for ASP folks

If you are planning on making a database call from the web the first thing you will need to do is plan on making a "Database Connection". Here are some sample database connection strings to help you get started.


Sample Database Connection Strings

Wednesday, June 27, 2007

Convert elements in multidimensional array to single dimensional array

function to_single_array(aelement,arr_col)
' convert multidimension array to single array
if isarray(aelement) then
for i=0 to ubound(aelement,2)
strval=strval & aelement(arr_col,i) & ","
next
strval=left(strval,len(strval)-1)
to_single_array=split(strval,",")
'response.write strval & "strval"
end if
end function

aelement is the name of multi dimensional array while arr_col is the column number which we want to converts into single array
this is the example of the multidimensional array

tr>


NameAge


Amy Jefferson

17




Mark Tan

25




John Grissam

29



Kathleen Rowling

35




so if the array name is ainfo, the way to extract the name into a single dimension array is
ainfo2=to_single_array(ainfo,0)

ainfo2 elements to will be:

Amy Jefferson
Mark Tan
John Grissam
Kathleen Rowling

any errors or feedback forward it to : nor@melur.com

Single Dimension Array concatenation

Single dimension array

Most of the time you will find that you need to concatenate 2 or more arrays.Somehow in classic asp there's no built in function that can combine array in one command. At least not that i know. Fill with the desperation to do it most of the time i wrote a simple function to concatenate 2 or more arrays

First i create a function call concat:

Function concat(array1,array2)
str1=join(array1,",")
str2=join(array2,",")
str3=str1 & "," &
str2 concat=split(str3,",")
end function

then I call the function anywhere when you need to concatenate arrays. For example, you want concatenate single dimension array

aitem1=array("A1","A1","A3")
aitem2=array("A4","A5","A6")

using this function:
aitem3=concat(aitem1,aitem2)

The elements for aitem3 are: "A1","A1","A3","A4","A5","A6"

I AM THE AUTHOR
Noraida Arifin
16 years  programming experience and  still learning.
spent 13 years of my life span as a software engineer
in a multinational company
Currently  a retire programmer.
Feedback are welcome.
Contact me at :
https://www.facebook.com/makcikprogrammer