Elxis CMS Forum

Support => General => Topic started by: ROUBOS on February 24, 2013, 15:49:08

Title: Your advice and opinions on a simple database application online. [SOLVED]
Post by: ROUBOS on February 24, 2013, 15:49:08
Hi,
I want to create a simple database (3 or 4 tables), and I want it to be online with user login so I thought the use of Elxis. First can it be done? And how should I go about achieving this.
Here is what I need to do.

I need to created an order tracking system. OK, so a Client's table to store client info. A product table. An order table.  And I need to have forms for the user to input a client, and then to place an order. And print some reports.
This is what happens.
The database user, will enter a new client into the database. Then in the order form he'll pick the client's name. He will pick from a drop-down list the product series, which filters and displays the product type. Then enter the amount. The program will calculate the amount of pallets required (depending on product series and type, different amount per pallet), and display that amount. The order can have a processed or delivered flag. Delivery dates etc etc. Print reports on today's orders etc. And just browse orders and change details as required.
So it is simple as a thought. Trying to set it up in Access 2010 now to see how it will work, but putting it online is a different story. Need to be able to have a user login to access this, so I thought that I could use an Elxis site.

Any thoughts, ideas, recommendations on how to tackle this will be appreciate. Is there an Elxis extension that could make my life easier?

Thanks in advance
Title: Re: Your advice and opinions on a simple database application online.
Post by: datahell on February 25, 2013, 08:51:00
CMSs in general, and in particularly Elxis CMS, have a big advantage: they provide you a full working platform pre-built so you dont have to re-invent the wheel. By using Elxis you can focus only on your application without caring on how to connect to the database, provide access, etc. My proposal is to use the Elxis database and create a small component for the application interface. A component can be very simple and if it is only for your own use you dont have to build fancy things. Off course you need some php knowledge but it is much less that the knowledge you would need if you build that application outside Elxis as a standalone one.

To get start fast follow these steps:
1. Go to Elxis database via phpmyadmin and create your tables. elx_mytable, ....
2. Go to table elx_components and add a new entry for your component (see existing components as samples)
3. Go to table elx_acl and add 2 entries for your component (see existing components as samples)
4. Go to folder components and create a sub folder with the name of your component (eg. com_track)
5. Create these files:
components/com_track/track.php
components/com_track/track.xml
These are the absolutely requires files a component may have.

Your component will be accesible like that: http://www.example.com/track/

6. Your component will have some tasks, like the form display and submission, view/print report, etc. See how other component are built as samples and ask here if you need help.

Title: Re: Your advice and opinions on a simple database application online.
Post by: ROUBOS on March 02, 2013, 07:22:00
thanks for the reply.

I've never coded in php. Found some easy to follow tutorials on creating html forms with php to enter data into the database and to view records.
How hard is it to create calculated fields (calculate depending on drop box selection and populate a text box with the result.). And how hard to populate one dropbox, depending on the selection of another dropbox.?
Title: Re: Your advice and opinions on a simple database application online.
Post by: datahell on March 02, 2013, 10:12:16
You can not develop any web application without knowing a programming language like PHP.
Chained select boxes can be created using javascript. You can use AJAX or not, this depends on the number of combinations between the 2 select boxes.

I will write you a quick sample code how to do it using simple javascript (The AJAX method is far more complex and I can't write such example here).
Select box "country" list countries and select box "cities" list cities.
When you change the country (onchange event) the loadcities javascript function is executed and loads the corresponding cities for the selected country.

The select boxes (Italy is selected by default)
<select name="country" id="country" onchange="loadcities();">
<option value="it" selected="selected">Italy</option>
<option value="gr">Greece</option>
</select>
<select name="cities" id="cities">
<option value="Milano" selected="selected">Milano</option>
<option value="Rome">Rome</option>
<option value="Torino">Torino</option>
</select>

The javascript (I use the DOM method for cross-browser compatibility)
function loadcities() {
      var cObj = document.getElementById('country');
      var pObj = document.getElementById('cities');
      for (var i= pObj.length; i > 0; i--) { pObj.remove(i-1); }
      if (cObj.options[cObj.selectedIndex].value == 'gr') {
            var option=document.createElement("option");
            option.text="Athens";
            option.value="Athens";
            pObj.add(option, null);
            option.text="Thessaloniki";
            option.value="Thessaloniki";
            pObj.add(option, null);
            option.text="Patra";
            option.value="Patra";
            pObj.add(option, null);
     } else {
            var option=document.createElement("option");
            option.text="Milano";
            option.value="Milano";
            pObj.add(option, null);
            option.text="Rome";
            option.value="Rome";
            pObj.add(option, null);
            option.text="Torino";
            option.value="Torino";
            pObj.add(option, null);
    }
    pObj.selectedIndex = 0;
}

Note
The cities can be put in an array and iterate through the array. You can use different arrays for each language, and in general improve the above script a lot.
Title: Re: Your advice and opinions on a simple database application online.
Post by: ROUBOS on March 03, 2013, 18:25:38
Thanks for your replies. Need your help with something here now. I managed to get things started using MySQL, PHP. (XAMPP locally, hpMyAdmin)
I created a database with tables, and html forms to populate the tables using php mysql.

The problem I'm having is in the relationships between tables.
I have 2 tables that look like this:

tblBlockSeries
~~~~~~~~~~~~~~~~~~~~~~
SeriesID (key, auto increment)
BlockSeries (varchar)

and

tblBlockType
~~~~~~~~~~~~~~~~~~~~~
TypeID (key, auto increment)
SeriesID (the idea was to use this to join the two tables)
BlockType (VARCHAR)
BlocksPerPallet (INT)

So I populated the table tblBlockSeries.
Trying to populate the table tblBLockType I have created a html form. Managed to populate a dropdown box with rows form the BlockSeries of the table tblBlockSeries. I want to select the BlockSeries from the dropdown box, and type in the fields for BlockType and BlocksPerPallet.

I cannot get the relationship right, so that when I choose a BlockSeries, the SeriesID to populate the Series ID of the tblBLockType table. Since One BlockSeries can have many BlockTypes. (one to many relationship.)

I hope I'm making sense.

Here is my code for the form to populate the table tblBlockType:
Code: [Select]
<html>
<body>

<form action="insert-blocktype.php" method="post">
<table>
<tr>
<td>Block Series:</td>
<td>
<?php
$link mysql_connect("localhost""db_user""password");
mysql_select_db("my_db"$link);

$query="SELECT SeriesID,BlockSeries FROM tblblockseries";

$result mysql_query ($query);
echo "<select name=blockseries=&#39;&#39;>Block Series</option>";

while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[SeriesID]>$nt[BlockSeries]</option>";
}
echo "</select>";
?>

</td>
</tr>
<tr>
<td>Block Type:</td>
<td><input type="text" name="blocktype"></td>
</tr>
<tr>
<td>Blocks Per Pallet:</td>
<td><input type="text" name="blocksperpallet"></td>
</tr>
<tr>
<td><input type="submit"></td>
</tr>
</form>

</body>
</html>

and here is the code for the insertion of the data into the table:
Code: [Select]
<?php
$link 
mysql_connect(&#39;localhost&#39;, &#39;db_user&#39;, &#39;password&#39;);
if (!$link) {
    die(&
#39;Could not connect: &#39; . mysql_error());
}
mysql_select_db("my_db"$link); 

$insert="INSERT INTO tblblocktype (BlockType, BlocksPerPallet)
VALUES
(&#39;
$_POST[blocktype]&#39;,&#39;$_POST[blocksperpallet]&#39;)";


if(
mysql_query($insert,$link)){
echo "Record added";
}else{
die(&#39;Error: &#39; . mysql_error());
}
mysql_close($link);
header(&#39;Location: &#39; . $_SERVER[&#39;HTTP_REFERER&#39;]);
?>

The SeriesID field in the tblBlockType table, should be populated based on the BlockSeries selection.

Isn't that how it should be working? Is my understanding of relationships wrong? Is that how keys and foreign keys work?
The SeriesID field in both tables should have the same value in order for the tables to be joined?

I'm using phpMyAdmin as a database tool.

I appreciate your help.
Title: Re: Your advice and opinions on a simple database application online.
Post by: ROUBOS on March 03, 2013, 18:38:09
I see all these attributes in phpMyAdmin like Index. An not sure how to set them up properly

There has to be something I'm not doing right in setting up a foreign key, and all those cascade etc options
Title: Re: Your advice and opinions on a simple database application online.
Post by: datahell on March 03, 2013, 23:13:43
Your naming is a little bit confusing.
The first thing to do is to focus on your target, what you want to do? Create 2 chained select boxes, right? This is your target.
I personally think of the database as a data store. I don't put much login on it, I prefer using php for that. Off course you need a common column in the 2 tables to make the join but this join can be done outside the database. How? Let's see.

Execute 2 queries and get the results as arrays.
The first array ($series) will have all the entries from table "tblBlockSeries" and the second ($types) from "tblBlockType".
$rows is an array we will create containing our final result.

Code: [Select]
$rows = array();
if ($series) {
//iterate through $series
foreach ($series as $ser) {
$id = $ser['SeriesID'];
$rows[$id] = array(
'BlockSeries' => $ser['BlockSeries'],
//types will contain the entries from the second db table
'types' => array()
);
}
//iterate through $types
if ($types) {
foreach ($types as $type) {
$id = $type['SeriesID'];
if (!isset($rows[$id])) { continue; } //wrong entry, dont use it
$rows[$id]['types'][] = array(
'BlockType' => $type['BlockType'],
'BlocksPerPallet' => $type['BlocksPerPallet']
);
}
    }
}

$rows is a multilevel array containing your final result. The 'types' array contains the "BlockTypes" for each "Block series".

Recommendation
Use lower case names for your db columns. This way you reduce a lot typing errors. PHP is case sensitive.
Title: Re: Your advice and opinions on a simple database application online.
Post by: ROUBOS on March 04, 2013, 02:41:36
kind of lost now.
I want to populate the second table.
The first table is populated with the series, and the second table I want to populate with types. One Series has many types.
Check out my tables screenshot.
Now the form I've created, populates a combo-box with the series from the series table, and I type in the other fields.

With a correct relationship setup, shouldn't the second table be populated with the id from the first table?
 
Title: Re: Your advice and opinions on a simple database application online.
Post by: datahell on March 04, 2013, 08:00:17
The solution to your problem is what I already wrote you. It is simple, an array containing the combined results that can be easily handled via php to generate the required html and javascript afterwards.

To answer your question: To join the 2 tables execute the query below.
SELECT a.*, b.BlockSeries FROM tblBlockType a INNER JOIN tblBlockSeries b ON b.SeriesID = a.SeriesID;
Title: Re: Your advice and opinions on a simple database application online.
Post by: ROUBOS on March 04, 2013, 08:07:34
OK,
I dropped those tables and started again. Re-created them and now the relationship seems to be fine. When I input data from within phpMyAdmin it works (looks like it) so I don't mind about this.
I guess I should ignore relationships and just use queries with php to get the data out and in the tables.

I've created an Order_Table and I need to create an order-form. Now this form requires the user to select BlockSeries with a dropdown box, which should populate another dropdown for BlockType depending on the BLockSeries selected.  (Isn't that what you pointed out? I'm trying to read your code  :-[)

Then populate the order-table.
Next create a report showing all orders from orders table, with options to edit or delete orders.

I'll get there eventually. :)

Title: Re: Your advice and opinions on a simple database application online.
Post by: ROUBOS on March 04, 2013, 08:25:47
The code you've given me creates two arrays with the data of both tables.
How do I populate a dropdown-box with data from the first array,
and then populate another dropdown-box depending on the first option.

Populate the second dropdown box block-type where series-id is the same as the series-id of the first dropdown box selection?
Title: Re: Your advice and opinions on a simple database application online.
Post by: datahell on March 04, 2013, 09:35:16
The code I wrote you generates 1 array ($rows), not 2. Use it to generate HTML and javascript as on my previous post (https://forum.elxis.org/index.php?topic=7536.msg48708#msg48708).

Example how to make html from $rows:

<select name="sieries" id="sieries" onchange="loadTypes();">
<?php
$selected = 1; //the default seriesID
foreach ($rows as $id => $row) {
    $sel = ($id == $selected) ? ' selected="selected"' : '';
    echo '<option value="'.$id.'"'. $sel.'>'.$row['BlockSeries'].'</option>';
?>
</select>
<select name="types" id="types">
<?php
if ($rows[$selected]['types']) {
   foreach ($rows[$selected]['types'] as $type) {
      echo '<option value="'.$type['BlockType'].'">'.$type['BlockType'].' '.$type['BlocksPerPallet'].'</option>';
  }
}
?>
</select>
Title: Re: Your advice and opinions on a simple database application online.
Post by: ROUBOS on March 05, 2013, 16:29:56
Thanks for your help.
You're right, needed some javascript with php to code this.

I managed to do it all ok. Now it all works. :)

The problem I'm having is that when I submit an empty form, it creates an empty entry in the database.
How can I simply test and make sure all fields have value in them?

also looked at this: http://www.blueicestudios.com/chained-select-boxes-using-php-mysql-ajax/
Title: Re: Your advice and opinions on a simple database application online.
Post by: datahell on March 05, 2013, 19:34:07
Once more the golden rule:
NEVER, EVER, USE DATA DERIVED FROM THE USER WITHOUT VALIDATING THEM FIRST.

You should filter and validate the data before doing anything else with them. Only if they are valid to insert them into the database.
I strongly advise you to follow be recommendation regarding filtering as you don't use PDO and you are not aware of security issues.

In a simple form, filtering/validating is like this (filter_input documentation (http://php.net/manual/en/function.filter-input.php), validate (http://www.php.net/manual/en/filter.filters.validate.php) and sanitize filters (http://www.php.net/manual/en/filter.filters.sanitize.php)).

$mystring = trim(filter_input(INPUT_POST, 'mystring', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW));
$myint = isset($_POST['myint']) ? (int)$_POST['myint'] : -1;

if (($mystring != '') && ($myint > -1)) {
    //insert data into the database (use $mystring and $myint, not the original values from $_POST !)
}

Depending on the data you wait to be submitted you validate the form fields respectively. For instance in the above example $myint is an integer with values 0, 1, 2, .... If you want to allow only values between 2 and 5 you will change the check to this:
if (($mystring != '') && ($myint > 1) && ($myint < 6)) {

You must be careful with text and strings.
If you want only the latin ascii chars you can strip out the HIGH ASCII chars too (dont use it on unicode strings):
$mystring = trim(filter_input(INPUT_POST, 'mystring', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH));

trim function is not unicode aware. You need a function like Elxis 4.x eUTF::trim to work with utf-8 encoded strings.
If you want $string to have at least 3 chars your validation should change to this:
if (($mystring != '') && (strlen($mystring) > 2) ....
strlen is also not utf-8 friendly. See Elxis 4.x eUTF::strlen

If you accept quotes in strings you must use addslashes, if in your server magic slashes are disabled, to escape them. Else you will be hacked! If you dont accept single/double quotes you should remove them with preg_replace first. backslashes, and characters like the following are also very dangerous: #, *, `, %

Some fields have only specific values. E.g. values from a select box can not be outside a range. So, there are safer ways to validate these.

Example:
$valid_colours = array('red', 'black', 'blue', 'purple');
if (in_array($colour, $valid_colours)) {
     //$ colour contains an acceptable value
} else {
    //$color contains a bad value
}

Final note
Be careful. Web applications built by amateurs, and especially outside a good framework such as Elxis, are most likely to be hacked... You must be very careful how you handle the user submitted data. This is why I initially adviced you to built an Elxis component and not a standalone application. Elxis includes already all the tools to handle the data easy and safe. Now you have to build these tools by yourself without having good php knowledge...
Title: Re: Your advice and opinions on a simple database application online.
Post by: ROUBOS on March 06, 2013, 01:18:05
thanks for your all your help.
I will look into the security issues and the data validation. I'm a noob when it comes to php so yes it is a problem.
I also created my tables with utf8_general_ci Collation. Shouldn't have since it is not multilingual. I never knew that it could cause such problems. (Being a HTML/CSS, Photoshop user).

What is MySQL's default collation? UTF8 is recommended right? Or go for a latin one? (Only english is used)

I read that latin1_swedish is the default so changed to that


Title: Re: Your advice and opinions on a simple database application online.
Post by: datahell on March 06, 2013, 08:29:47
The charset is for the stored data, the collation is only for ordering and other string released functions.

Tables charset = utf-8
Collation: utf8_general_ci

I still advise you to develop an Elxis component instead of a standalone script...
Title: Re: Your advice and opinions on a simple database application online.
Post by: webgift on March 06, 2013, 11:37:34
Roubos, if you're familiar with Object oriented PHP programming, please take a look to the Elxis Framework. For Elxis CMS 4.x take a look to this article (https://www.elxis.net/docs/developers/libraries/efactory.html). There are so many methods that you can use ...
Title: Re: Your advice and opinions on a simple database application online.
Post by: ROUBOS on March 19, 2013, 09:27:29
Hi again,
I need advice on this code I've written.

I insert into an array the table content, and then I populate a html table using the array.
Now I want to do some calculations with the values of some <td> elements. The problem is that I'm trying to run the calculation (javascript) onfocus. It only works if I do it in order, and does not work when I randomly click on the element. Does not work when I click on it again once I've made changes.
Here is my PHP code:
Code: [Select]
<div style="float:left;"> <!-- DIV creating table and populating with 100 Series -->
<?php
$sql "SELECT * FROM blocks_table WHERE series=&#39;100 Series&#39;";
$result mysql_query($sql)or die(mysql_error());
$rowcounter=0;

echo "<table class=&#39;styled-table&#39;>";
echo "<tr><th>100 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>";
while($row mysql_fetch_array($result)){
$type100 $row[&#39;type&#39;];
$perpallet100 $row[&#39;per_pallet&#39;];
$rowcounter++;
// Now for each looped row
 
echo "<tr><td>".$type100."</td>
<td><input type=&#39;text&#39; name=&#39;perpallet&#39; id=&#39;perpallet
$rowcounter&#39; style=&#39;width:50px;&#39; value=&#39;".$perpallet100."&#39;/></td>
<td><input type=&#39;text&#39; name=&#39;quantity&#39; id=&#39;quantity
$rowcounter&#39; style=&#39;width:50px;&#39;/></td>
<td><input type=&#39;text&#39; name=&#39;pallets&#39; id=&#39;pallets
$rowcounter&#39; style=&#39;width:50px;&#39; onfocus=&#39;this.value=getTotal();&#39;/></td>
<td><input type=&#39;text&#39; name=&#39;extras&#39; style=&#39;width:50px;&#39;/></td>
</tr>"
;
// End our while loop
echo "</table>";
?>


and here is the javascript:
Code: [Select]
<!-- calculation -->
<script type="text/javascript">
var kavli = 0;

function getTotal()
{
var total = 0;
kavli++;
var blockamount = 0;
var perpallet = 0;

blockamount = document.getElementById('quantity' + kavli);
perpallet = document.getElementById('perpallet' + kavli);

total = Math.floor(blockamount.value / perpallet.value);

return total;
}
</script>
<!-- end calculation -->

So a table row, shows a block-type, which has a per-pallet amount (how many blocks fit on a pallet), then I enter the Quantity of the blocks I want, and it should calculate the amount of pallets needed.

Simple really, but I cannot get it to work.

I think it does not work cause it's going through loops.

Any thoughts? An advice on how I should tackle this?

thanks for all your help.
I attached a screenshot of the table

EDIT: I used variables auto-increment to be able to give the html input types unique ids.
Works only if I enter an amount on the first field, then click on the next field. All in order.

When I randomly try to do it I get "0".
Title: Re: Your advice and opinions on a simple database application online.
Post by: datahell on March 19, 2013, 19:06:14
Never iterate though results like that. First collect the results, count them and then build the html.
Also your HTML sucks... Use double quotes for element attributes and add the "value" attribute in "text" elements (you have it only on the first element). Also you have multiple elements with the same name (use different IDs but "name" is the same). For compatibility with the standards these elements must be inside a FORM tag.


<td><input type="text" name="perpallet1" id="perpallet1" value="xxx" /></td>
<td><input type="text" name="quantity1" id="quantity1" value="xxx" /></td>
<td><input type="text" name="pallets1" id="pallets1" value="xxx" onfocus="getTotal(1);" /></td>
<td><input type="text" name="extras1" id="extras1" value="xxx" /></td>

<td><input type="text" name="perpallet2" id="perpallet2" value="xxx" /></td>
<td><input type="text" name="quantity2" id="quantity2" value="xxx" /></td>
<td><input type="text" name="pallets2" id="pallets2" value="xxx" onfocus="getTotal(2);" /></td>
<td><input type="text" name="extras2" id="extras2" value="xxx" /></td>

function getTotal(idx) {
   var ppidx = 'perpallet'+idx;
   var qidx = 'quantity'+idx;
   var xidx = 'pallets'+idx;
   if (!document.getElementById(pidx)) { return false; }
   if (!document.getElementById(qidx)) { return false; }
   if (!document.getElementById(xidx)) { return false; }
   var p = document.getElementById(pidx).value;
   if ((p == '') || !isNaN(p)) { return false; }
   var q = document.getElementById(qidx).value;
   q = parseInt(q, 10);
   var total = Math.ceil(q / p);
   document.getElementById(xidx).value = total;
}

Title: Re: Your advice and opinions on a simple database application online.
Post by: ROUBOS on March 20, 2013, 02:39:33
Hi,
thanks for the help and your time going through this.

The double quotes for element attributes where not used because it all sits inside a php echo and its the only way I could get it to work.
The name part of the elements and the value is a constant delete/rewrite because of my luck of php knowledge, trying to get it to work I keep deleting re-writing things. And they are all inside form tags. Sorry I did not copy and paste my entire code.


here is my entire page as it was when I posted it before:
Code: [Select]
<?php
// connect to the database
include(&#39;db.php&#39;)
?>

<html>
<head>
<!-- calculation -->
<script type="text/javascript">
var kavli = 0;

function getTotal()
{
var total = 0;
kavli++;
var blockamount = 0;
var perpallet = 0;

blockamount = document.getElementById('quantity' + kavli);
perpallet = document.getElementById('perpallet' + kavli);

total = Math.floor(blockamount.value / perpallet.value);

return total;
}
</script>
<!-- end calculation -->
<title>HB - Block Order Tracking System</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<img class="page-bg" src="page-bg.jpg">
<img class="header-img"src="header-bg.png">
<div id="wrap">
<center>
<form action="insert-order.php" method="post">
<table class="styled-table">
<tr>
<th>Order Date</th> <th>Company Name</th><th>Delivery Date</th>
</tr>
<tr>
<td><input type="date" name="orderdate"></td>
<td>
<?php
$query mysql_query("SELECT company_name FROM client_table ORDER BY company_name");
echo "<select name=users>";
echo "<option value=blank selected></option>"//for blank default option
while ($r mysql_fetch_array($query))
{
$client $r["company_name"];
echo "<option value=$client>$client</option>";
}
echo "</select>";
?>

</td>
<td><input type="date" name="deliverydate"></td>
</tr>
</table>
<br />
<div style="float:left;"> <!-- DIV creating table and populating with 100 Series -->
<?php
$sql "SELECT * FROM blocks_table WHERE series=&#39;100 Series&#39;";
$result mysql_query($sql)or die(mysql_error());
$rowcounter=0;

echo "<table class=&#39;styled-table&#39;>";
echo "<tr><th>100 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>";
while($row mysql_fetch_array($result)){
$type100 $row[&#39;type&#39;];
$perpallet100 $row[&#39;per_pallet&#39;];
$rowcounter++;
// Now for each looped row
 
echo "<tr><td>".$type100."</td>
<td><input type=&#39;text&#39; name=&#39;perpallet&#39; id=&#39;perpallet
$rowcounter&#39; style=&#39;width:50px;&#39; value=&#39;".$perpallet100."&#39;/></td>
<td><input type=&#39;text&#39; name=&#39;quantity&#39; id=&#39;quantity
$rowcounter&#39; style=&#39;width:50px;&#39;/></td>
<td><input type=&#39;text&#39; name=&#39;pallets&#39; id=&#39;pallets
$rowcounter&#39; style=&#39;width:50px;&#39; onfocus=&#39;this.value=getTotal();&#39;/></td>
<td><input type=&#39;text&#39; name=&#39;extras&#39; style=&#39;width:50px;&#39;/></td>
</tr>"
;
// End our while loop
echo "</table>";
?>

</div>
</form>
</center>
</div>
</body>
</html>

Unfortunately I don't know php and trying to do this by google-ing and reading here and there.
 
Title: Re: Your advice and opinions on a simple database application online.
Post by: datahell on March 20, 2013, 19:21:07
Check this.

Code: [Select]
<?php
include(&#39;db.php&#39;)
?>

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>HB - Block Order Tracking System</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
<script type="text/javascript">
function getTotal(idx) {
var ppidx = 'perpallet'+idx;
var qidx = 'quantity'+idx;
var xidx = 'pallets'+idx;
if (!document.getElementById(pidx)) { return false; }
if (!document.getElementById(qidx)) { return false; }
if (!document.getElementById(xidx)) { return false; }
var p = document.getElementById(pidx).value;
if ((p == '') || isNaN(p)) { return false; }
var q = document.getElementById(qidx).value;
q = parseInt(q, 10);
var total = Math.ceil(q / p);
document.getElementById(xidx).value = total;
}
</script>
</head>
<body>
<img class="page-bg" src="page-bg.jpg" alt="bg" />
<img class="header-img" src="header-bg.png" alt="header" />
<div id="wrap">
<form name="fmorder" action="insert-order.php" method="post">
<table class="styled-table">
<tr>
<th>Order Date</th><th>Company Name</th><th>Delivery Date</th>
</tr>
<tr>
<td><input type="text" name="orderdate" value="" /></td>
<td>
<?php
$query mysql_query("SELECT company_name FROM client_table ORDER BY company_name");
echo &#39;<select name="users">&#39;;
echo &#39;<option value="" selected="selected"></option>&#39;;
while ($r mysql_fetch_array($query)) {
echo &#39;<option value="&#39;.$r[&#39;company_name&#39;].&#39;">&#39;.$r[&#39;company_name&#39;]."</option>";
}
echo "</select>";
?>

</td>
<td><input type="text" name="deliverydate" value="" /></td>
</tr>
</table>
<br />
<div style="float:left;">
<?php
$sql "SELECT * FROM blocks_table WHERE series=&#39;100 Series&#39;";
$result mysql_query($sql)or die(mysql_error());
$rowcounter 0;
echo &#39;<table class="styled-table">&#39;;
echo &#39;<tr><th>100 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>&#39;;
while($row mysql_fetch_array($result)){
$rowcounter++;
echo &#39;<tr><td>&#39;.$row[&#39;type&#39;].&#39;</td>
<td><input type="text" name="perpallet&#39;.$rowcounter.&#39;" id="perpallet&#39;.$rowcounter.&#39;" style="width:50px;" value="&#39;.$row[&#39;per_pallet&#39;].&#39;" /></td>
<td><input type="text" name="quantity&#39;.$rowcounter.&#39;" id="quantity&#39;.$rowcounter.&#39;" style="width:50px;" value="" /></td>
<td><input type="text" name="pallets&#39;.$rowcounter.&#39;" id="pallets&#39;.$rowcounter.&#39;" style="width:50px;" value="" onfocus="getTotal(&#39;.$rowcounter.&#39;);" /></td>
<td><input type="text" name="extras" value="" style="width:50px;" /></td>
</tr>&#39;;
}
echo &#39;</table>&#39;;
?>

</div>
</form>
</div>
</body>
</html>
Title: Re: Your advice and opinions on a simple database application online.
Post by: ROUBOS on April 05, 2013, 04:48:41
Hi, thanks for your reply and taking your time to look at this.
I've been away so I only got the chance to look at it again now.

It's not working at the moment. The code you've given me looks awesome. Been going through it to understand the logic and the way it flows.

The loop goes through and draws the table fine. But of-course it does not populate the "Pallets" field cause there is nothing in the "Quantity" field. And the onfocus does not work to display the total, since we're out of the loop once the page loads and cannot access the getTotal() function.
So I should try to draw the table with the loop, and then listen for the onfocus and call the getTotal function.

That's my understanding of it.

Thanks again :)


EDIT: NO, looking through the code it all seems fine. The loop is not the problem. It does draw the table giving unique names and ids. So the javascript function is called, not getting a result.
looks like the onfocus might not be working. reading online regarding this now, and people have been have strange results with "document.getElementById" too

Title: Re: Your advice and opinions on a simple database application online.
Post by: ROUBOS on April 10, 2013, 11:07:39
It works when I comment out: if ((p == '') || !isNaN(p)) { return false; }

Thanks Datahell for all your help. I really appreciate it.

It all works fine. I commented this line of code, and all works as it should be. I was trying to locate the problem using alert messages.

Thanks again :)

PS: should this line of code be written like this?:  if ((p == '') || isNaN(p)) { return false; } without the "!"
Title: Re: Your advice and opinions on a simple database application online.
Post by: datahell on April 10, 2013, 13:30:36
Yes, sorry, !isNAN is 2 negatives in a row which makes a logical positive. The correct is isNaN without the NOT symbol (!)
Title: Re: Your advice and opinions on a simple database application online.
Post by: ROUBOS on April 10, 2013, 15:00:59
Elxis is great and what makes it even better is the great support here in this forum.

Thanks once again Datahell.
Appreciate your help.

Ευχαριστώ πολύ.
Title: Re: Your advice and opinions on a simple database application online. [SOLVED]
Post by: ROUBOS on April 16, 2013, 10:03:30
Hi once again,
I've populated a mysql table with form data before, but it was a single row at the time from a simple form.

The form we build here (from this post), will have to populate two different tables. (I've attached an image of the mysql tables).
The first table is straight forward to insert the data from the form, but the second table gets populate with many rows of data. (I've attached what the form looks like)

So I guess I need to loop through the html tables and insert rows into the mysql table. Or do I read until end of form?

Do I populate the mysql table with the data from the form with even the blank (zero) fields? In case we want to display the stored orders and edit them, we'll have to re-create this form with data from the table.

thanks... just thought I post here again while I'm trying to sort this out.

My code so far, the data that needs to go to the first table is easy to insert. Now for the rest :/ :
Code: [Select]
<?php
 
// connect to the database
 
include(&#39;db.php&#39;);

$insert="INSERT INTO order_table (company_name, delivery_address, order_date, delivery_date, status, hb_checked, loaded_by, pallets, extra_blocks, extra_pallets, total_pallets)
VALUES
(&#39;
$_POST[companyname]&#39;,&#39;$_POST[deliveryaddress]&#39;,&#39;$_POST[orderdate]&#39;,&#39;$_POST[deliverydate]&#39;,&#39;$_POST[status]&#39;,&#39;$_POST[checked]&#39;,&#39;$_POST[loadedby]&#39;,&#39;$_POST[sumpallets]&#39;,&#39;$_POST[sumextras]&#39;,&#39;$_POST[makeup]&#39;,&#39;$_POST[grandtotal]&#39;)";
?>


<html>
<head>
<title>HB - Block Order Tracking System</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<img class="header-img"src="header-bg.png">
<div id="wrap">
<?php
if(mysql_query($insert)){
echo "Record Added";
}else{
die(&#39;Error: &#39; . mysql_error());
}
?>

<div id="centerDiv">
<ul class="centerUL">
<li><a href="index.html" class="button">Back</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>

I will have to load all data from the html tables into an array? and use this array to populate the mysql table?
Title: Re: Your advice and opinions on a simple database application online.
Post by: ROUBOS on April 20, 2013, 11:40:06
GOT IT  :D
Code: [Select]


<?php
include(&#39;db.php&#39;); //connect

echo &#39;<pre style="text-align: left;">&#39; . print_r($_POST, true) . &#39;</pre>&#39;;

mysql_query("INSERT INTO order_table (company_name, delivery_address, order_date, delivery_date, status, hb_checked, loaded_by, pallets, extra_blocks, extra_pallets, total_pallets)
VALUES
(&#39;
$_POST[companyname]&#39;,&#39;$_POST[deliveryaddress]&#39;,&#39;$_POST[orderdate]&#39;,&#39;$_POST[deliverydate]&#39;,&#39;$_POST[status]&#39;,&#39;$_POST[checked]&#39;,&#39;$_POST[loadedby]&#39;,&#39;$_POST[sumpallets]&#39;,&#39;$_POST[sumextras]&#39;,&#39;$_POST[makeup]&#39;,&#39;$_POST[grandtotal]&#39;)");

$currentid mysql_insert_id();

foreach(
$_POST[&#39;100type&#39;] AS $key=>$value) {
$sql "INSERT INTO order_details_table
(order_id, block_type, block_quantity, block_pallets, block_extras)
VALUES (&#39;
$currentid&#39;, &#39;{$_POST[&#39;100type&#39;][$key]}&#39;,&#39;{$_POST[&#39;100quantity&#39;][$key]}&#39;,&#39;{$_POST[&#39;100pallets&#39;][$key]}&#39;,&#39;{$_POST[&#39;100extras&#39;][$key]}&#39;)";

$result mysql_query($sql) or die(&#39;Could not insert data&#39;);
}

?>


now need to work on security in all my code