php mysql - document retrieveal and display - php

I built a document upload admin screen, where my client can browse and upload pdf documents to a mysql dbase.
I have two separate tables for the Agendas, and one for Minutes. both have separate table names "upload" and "upload_mins".
on the index.php page, I have the page fetch each row of the database, and display all of the valid documents on a "download" page.
I have come a across a problem.
Each dbase is set to increment ID. now that there is two databases, they are coming to utilize the same ID.
so I am pulling from the Agenda table:
http://www.example.com/clerk.php?ID=77
and I am pulling from the Minutes table also:
http://www.example.com/clerk.php?ID=77
and they happen to have the same increment ID.
Is there some way to avoid this? Can I add a field parameter to the minutes to make sure that they don't have the same URL when pulling documents?
Create a integer field, or txt field?
i.e. http://www.example.com/clerk.php?ID=77&min=yes

If these are just documents, you could store them in a single table but have a column called type that differentiates between minutes and agendas. That way, IDs will be unique.
You could also extend the types column be a foreign key to a types table, so you can extend it to include additional document types such as spreadsheets in the future easily.
This would also aid the front-end display, as you would only need one query to fetch the documents within a batch or timeframe, but add them to differing arrays. For example:
// get the last 20 uploaded documents
$sql = "SELECT * FROM documents ORDER BY created DESC LIMIT 0,20";
$res = mysql_query($sql);
while ($doc = mysql_fetch_object($res)) {
$type = $doc->type;
if (!isset($docs[$type]) || !is_array($docs[$type])) {
$docs[$type] = array();
}
$docs[$type][] = $doc;
}
// loop over agendas
foreach ($docs['agendas'] as $agenda) {
echo '' . $agenda->title . '';
}
// loop over minutes
foreach ($docs['minutes'] as $minutes) {
echo '' . $minutes->title . '';
}
...

You say that the problem you are having is with URLs being duplicated. You have 2 different options to solve that.
The first is to create an identifier that tells you which table you want. You could have agenda.php and minutes.php, or you could have clerk.php?ID=77&type=1 and clerk.php?ID=77&type=2.
This will allow you to know which table you are referencing. This is also the approach I would recommend. There is no reason to avoid duplicate keys in different tables as long as you have a way of knowing which table you need.
The second option is to put all your records into a single table. You can then add a type column that specifies what type of document it is. You can then leave your clerk.php file alone, but need to change the upload page to populate the type column.
If you are just storing PDFs in the database, you shouldn't need anything in this table except id, type, and the document itself.

Related

How would you order by two columns in MYSQL Database?

I have a complex problem that I have been working on for a while. I don't even know if this is possible to do.This is an iOS app where users can reorder rows in the tableview and it updates the database (ordering02) with the temporary indexpath.row.
Columns to order by:
ordering01 : Products are ordered 1..2..3... and so on
ordering02 : When product is moved to a new position (indexPath.row) in the app this temporarily holds the new position in the mysql database until the user saves changes. Once the user saves changes that position is then saved into ordering01.
What I am trying to do is display the unsaved product order in the app (tableView). To do this I will need to sort by both ordering01 & ordering02. The complicated part is that I need the query to check if ordering02 exists. If ordering02 exists then that's the column that is used but if it doesn't exist then the column used is ordering01.
$sql = mysql_query("SELECT * FROM products WHERE status = '1' ORDER BY [Check if ordering 02 exists, if it does use ordering 02 if not use ordering01] ");
while($row = mysql_fetch_array($sql01)){
}
Does this Make sense and is it even possible?
For the temporary storage, why not use arrays, until the user clicks the save, then take from the array and insert into your table.
The best way to merge the two tables, is using the "JOINS" command in your SQL code and checking if the table exists, PHP offers a call method called "exists()", so you would first call the name of the table (ordering02) inside your if statement.
So you get:
$con = mysqli_connect(...);
$tblName = "ordering02";
If (exists($tblName)){
# Perform action
}else{
# table doesn't exist
}

Dynamic Table Rows with PHPWord

I am hoping someone can help me because I am attempting to do something that is beyond my limits, I don't even know if a function exists for this within PHP or MySQL so my search on google hasn't been very productive.
I am using PHPWord with my PHP/MySql Project, the intention is that I want to create a word document based on a template.
I have used this guide which is also on stack exchange.
However this approach requires that the number of rows and the values are hard coded, i.e. in his example he has used cloneRow('first_name', 3), which then clones the table to have 3 rows, and then goes on to manually define the tags, i.e.
$doc->setValue('first_name#1', 'Jeroen');
$doc->setValue('last_name#1', 'Moors');
$doc->setValue('first_name#2', 'John');
I am trying to make this dynamic, in my instance I am trying to make a timetable, and one of the child tables is exactly that, so the query I have looks up how many entries there are and then collects a count of them, this $count is then used to dynamically create the correct number of rows. This is the count I am using:
$rs10 = CustomQuery("select count(*) as count FROM auditplanevents where AuditModuleFk='" . $result["AuditModulePk"]."'");
$data10 = db_fetch_array($rs10);
$Count = $data10["count"];
I then use this $document->cloneRow('date', $Count); to executive the clonerow function, which works great and my document now looks something like this.
So, so far so good.
What I now want is for a way to then append each row value of the query into the document, so rather than manually setting the tag value i.e. $doc->setValue('first_name#1', 'Jeroen'); I could use something like $doc->setValue('first_name#1', '$name from row 1'); I suspect this will involve a foreach query but not too sure.
I hope the above makes sense, but please feel free to ask me for anything else and become my personal hero. Thanks
Update: Just for sake of clarity, what I would like is for the output to look something like this:
In my example are 5 results and therefore 5 rows created, I want to set values in following way:
${$date1} = date column from query 1st row
${$date2} = date column from query 2nd row
${$date3} = date column from query 3rd row
${$date4} = date column from query 4th row
${$date5} = date column from query 5th row
I was able to sort this out by inserting the records from the query into a temp table, with an AI ID, then using:
//update timetable with events from temp table
$rs14 = CustomQuery("select * FROM tempauditplan where AuditModuleFk='" . $result["AuditModulePk"]."'");
while ($data14 = db_fetch_array($rs14))
{
$document->setValue('date#'.$data14["rowid"], date('d/m/y', strtotime($data14["date"])));
$document->setValue('time#'.$data14["rowid"], date('H:i', strtotime($data14["time"])));
$document->setValue('auditor#'.$data14["rowid"], $data14["auditor"]);
$document->setValue('area#'.$data14["rowid"], $data14["area"]);
$document->setValue('notes#'.$data14["rowid"], $data14["notes"]);
$document->setValue('contact#'.$data14["rowid"], $data14["contact"]);
}
The trick is to also have a function that truncates the table after use so can be used over again
Might not be the most efficient way, but it works!

storing arrays into mySQL with php explode()

I am making a website for a cars show, i want to store images in the database (just the URL) and what i want to do is for all the images to be added to the same cell in the table.
then at retrieval time, i want to use the explode() command in php so i can seperate each URL and use a loop to display them.
the problem i am facing it i do not know what i should use for a delimiter, i cannot use anything that can be used in windows, mac or Linux which can be used in a file name, and i am afraid of using a system reserved key and cause a problem.
i am also concerned about the data type that will hold this information, i am thinking TEXT is best here but i heard many saying it causes problem.
to be clear, the idea is:
when someone uploads 3 images, the images will be uploaded into a folder, then the names will be taken and put into 1 string (after the directories names are added) with a separator between them that then will be stored in the database.
Then, i take that string, use explode() and store the separated data in an array and use a loop to display an image with the source being the stored data in the array.
i need a special delimiter or another way... can someone help me do this or tell me another way of saving the images somehow without a potential risk! i have seen many website which uses dynamic bullet points (lists) but i was never able to get a code or even an idea of how to do them.
EDIT:
The current way i am using is having 10 rows, 1 for each image.. but the problem here is that the user will not be able to add more than 10 images, and if he has less than 10 images then there will be few empty images being displayed. (i know a solution for the empty images, but it is impossible to add more images..)
You can to use any type of
serialization(serialize, json_encode), when put your array and
deserialization(unserialize, json_decode), when want to use it.
But! I advice you to create a new table for your images, with car_id field, for example.
Then you can just join it and get it all.
It can be CarImages ('id', 'car_id', 'image_file')
Also I recommend to add foreign key constraint on CarImages.car_id->Cars.id,
then your orphaned images will cascade removed, when cars will removed.
Storing of serialized values is always bad idea.
If you can't store one row per image on a separate table for any technical debt reason, then you should json_encode the array on images paths and store the result in database.
Solution one :
Create a table called Images contains 3 columns (id,image_path,user_id) and everytime the user uploads an image insert it into this table, and in your script if you want to display the uploads for a specified user get it by the user_id :
$user_id = 1 // The specified user id;
mysqli_query($connect, "SELECT * FROM images WHERE user_id = '$user_id'");
// And Loop here trough images
Solution Two :
Inserting images paths into one column.
Table files contains 1 column called path
Inserting the values to the files table :
$array = array(
'/path/to/file/1',
'/path/to/file/2',
'/path/to/file/3'
);
foreach($array as $path) {
$path = $path . '|';
mysqli_query($connect, "INSERT INTO files (path) VALUES ('$path');
}
Now display the results :
$query = mysqli_query($connect, "SELECT path FROM files");
$row = mysqli_fetch_assoc($query);
$paths = explode('|', $row['path']);
foreach($paths as $path) {
echo $path . '<br>';
}
If you do not change your database then you should try.I think below link useful for you
json
json-encode serialize
you can use anyone.
If You design Your Tables like
Table-user
user_id
username
another table for user images
Table-images
serial_num
user_id
image_url
then you can store many images for 1 user.here user_id of images table actually the foreign key of user table's user_id.
You are using relational database so it's good for you otherwise you can use nosql database

PHP CSV Import Questions

I'm working with importing CSV files into a database, and it is a multi-step process.
Here are the steps:
Step 1: User uploads a CSV file.
Step 2: User associates the data to a data type. For example, if a record in the CSV contains the following data: John,Doe,jondoe#gmailcom, the user would select firstname from a dropdown box to associate to the data value John, lastname from a dropdown box that associates to the data value Doe, and emailaddress from a dropdown box that associates to the data value johndoe#gmail.com
Step 3: Insert data into database
My questions are the following:
1./ On step 3, I would have in my possession the columns which the user chose and the original data.
Here is what the original data looks like:
$data = array(
0 => array('John','Doe','johndoe#gmail.com'),
1 => array('Foo','Bar','foobar#gmail.com')
);
And here is what my columns chosen from step 2 looks like:
$columns = array('firstname','lastname','emailaddress')
How do I create a sql query that can be like the following:
INSERT into contacts (id,firstname,lastname,emailaddress) values (null,'John','Doe','johndoe#gmailcom')
As you can see, the sql query has the columns chosen in the order that they are within the array and then subsequently the values. I was thinking that since the columns are chosen in the order of the data, I can just assume that the data is in the correct order and is associated to the specific column at that position (for example, I can assume that the data value 'John' was associated to the first position of the columns array, and vice versa).
2./ I was thinking of a possible scenario that when the user does the initial upload of the file, they could potentially send a csv file with the first record having a blank field. The problem is, I determine how many columns to have the user associate to the data based on the number of columns within a csv record. In this case, we have 2 columns and every subsequent record has 3 columns. Well, I'm not going to loop through the entire set of records to determine the correct number of columns. How do I resolve this issue? Any ideas?
EDIT
I think I figured out the answer to question 2. On the parsing of the csv file, I can get a count for each record and the highest count at the end of the parsing is my count. Seems right? Any issues with that?
To parse the data from the CSV file, look at fgetcsv. http://php.net/manual/en/function.fgetcsv.php
It'll load a line from the file and return an array of the CSV fields.
$data = array();
while (($lineFields = fgetcsv($handle)) !== false) {
$data[] = $lineFields;
}
This assumes you are using PHP5 and opened the file with $handle. In PHP4 fgetcsv needs a second parameter for max length of line to read.
For the query:
$sql = "INSERT into contacts (id," + implode(',', $columns) + ") values";
I'm not including anything after the values. You should be creating prepared statements to protect against sql injections. Also if you are using MySQL, id should be an autoincrement field and omitted from inserts (let MySQL generate it). If you are using Postgres, you'll need to create a sequence for the id field. In any case let the database generate the id for you.

Dynamic textbox creation with db using ajax/javascript/php

I have two tables. company_details and company_specials. Each company_details can have multiple specials. I display the company details at http://eurothermwindows.com/ed/admin.php
The first row and fourth row that has the 0 in the active column is from company_details and the rows below are from company_specials.
Currently the code allows for dynamic modification of the company_details rows as denoted by the compid in that table. However i would like to have the rows below it to be dynamically modified as well but it's using the same compid and i'm not sure how to separate them in the code.
Code below is the code being generated for the company_specials. I need a way to uniquely identify each row and be able to modify it.
http://pastebin.com/RAe9iwAP
Could somebody provide some guidance please? I'm thinking that i would probably need to uniquely identify each of the specials within the company_specials or set some sort of pointers?
Add unique ids to your db tables and output hidden text fields with each record to indicate it table origin and its id. this will allow the code to know which table had which row updated or deleted. inserting new records can be accomplished by offering a blank record of each type at the end of each group, so there would be one blank specials record at the end of each group and one blank company record at the end of the table.
Put a unique name on each input field of the form name='comp[<?php echo $comp_id?>][<?php echo $comp_field_name?>]' and name='spec[<?php echo $spec_id?>][<?php echo $spec_field_name?>]' so that when the table is posted, PHP will see two arrays, $comp and $spec. You can loop over these with
foreach ($comp as $id=>$row)
{
}
and loop over each $row to build an SQL update or insert statement with
foreach ($row as $fld=>$val)
{
}
Seems you're on the right track. You will indeed need to identify the rows uniquely. You could add a unique id to your company_specials table and use that. Or use some other combination of attributes that's unique to each row in your company_specials table (e.g. concatination of compid & specdate). Whatever fits your information-structure. I would advise to just add a unique id to your specials table.
Be sure to also add that unique-constraint to your database-system to prevent invalid data from being entered.

Categories