|
-
Mar 26th, 2009, 05:50 AM
#1
Thread Starter
Addicted Member
[RESOLVED] multidimensional csv linking - no idea?!
I have a spreadsheet that I can view with the power of PHP!...
php Code:
<?PHP
$file_handle = fopen("library.csv", "r");
while (!feof($file_handle) ) {
$text = fgetcsv($file_handle, 1024);
print $text[1] . " . " . $text[2] . "<BR>";
}
fclose($file_handle);
?>
But what i really what to do is sort the information dependant on what the user selects.
My first column has "authers" (most repeated) If one auther is selected i want column two to only show books by that auther.
So can I create a link (mousedown varable) with a slice of information from a csv file?
And can I switch on/off rows in a csv file?
Thank for any help or direction!
-
Mar 26th, 2009, 10:34 AM
#2
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
I think I have Identified how to link...
PHP Code:
print "<a href=\"text1down\">$text[1]</a> . <a href=\"text2down\">$text[2]</a><BR>";
but i'm strugling now on how to seporate rows in my csv file as aposed to columns to which there are plenty of tutorials!
Last edited by LingoOutsider; Mar 30th, 2009 at 01:43 AM.
-
Mar 26th, 2009, 11:19 AM
#3
Re: multidimensional csv linking - no idea?!
well, first you need to make sure that fgetcsv() has a length which is longer than the longest line that will be in your csv file (if you haven't done this already). I'm not sure how you're having trouble with separating rows from columns; every time your loop loops, it will be a new row.
now, since all you're doing is reading a file, you're a little out of luck for easy sorting methods. because you'll be dealing with arrays, this will get really ugly, really fast if you plan on sorting them. all I know of for dealing with your situation is to create multi-dimensional arrays with keys that can be used with array sort functions while you're creating your file, and displaying it by looping through this array after doing a sort.
honestly, if speed and ease of maintaining this code is even somewhat important to you, I would suggest just dumping all of this information into a database with a cron job/scheduled task (assuming that the data file will change periodically and that you'll want to keep a record of all of it) and use the database's built in sort functions. if you don't see this as something practical for what you're doing, however, let me know and I may be able to help you with some sort of array thing.
-
Mar 27th, 2009, 04:24 AM
#4
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
Thanks for the reply, I'm not 100% on how a cron job/scheduled task would be helpful my coding skills seem to be improving (if a bit basic). This is were i am at the moment...
PHP Code:
global $count_row; //avoid error during testing
$file_handle = fopen("library.csv", "r"); //open the file $grid_array_down = array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20); while (!feof($file_handle) ) { $text = fgetcsv($file_handle, 1024 ); //get info from file // number rows $count_row = $count_row + 1; // show info + link print "<a href=\"info3.php?page=$grid_array_down[$count_row]:$text[1]:$text[2]:$text[3]:$text[4]:$text[5]\">($grid_array_down[$count_row] , $text[1])</a> <a href=\"info3.php?page=$grid_array_down[$count_row]:$text[1]:$text[2]:$text[3]:$text[4]:$text[5]\">($grid_array_down[$count_row] , $text[2])</a> <a href=\"info3.php?page=$grid_array_down[$count_row]:$text[1]:$text[2]:$text[3]:$text[4]:$text[5]\">($grid_array_down[$count_row] , $text[3])</a> <a href=\"info3.php?page=$grid_array_down[$count_row]:$text[1]:$text[2]:$text[3]:$text[4]:$text[5]\">($grid_array_down[$count_row] , $text[4])</a> <BR>";
$mousedown = $_GET["page"]; //record the link pressed $stagelist = explode(":",$mousedown); // chop up into an array } if ($mousedown) { print_r ($stagelist); //test array } fclose($file_handle);
I don't mind replecating code for each row/column in the csv file even if it looks a bit messy.
I've used...
$mousedown = $_GET["page"];
$stagelist = explode(":",$mousedown);
...command to identify and seperate strings in rows, but I'm having difficalty dividing-up strings in columns any advice?
Then I'll look to get my head around the math and identify a command to hide (turn on/off) a row in the csv file
tidy the links and I think I'll be there.
Last edited by LingoOutsider; Mar 30th, 2009 at 01:42 AM.
-
Mar 27th, 2009, 10:41 AM
#5
Re: multidimensional csv linking - no idea?!
filtering your results is easy if you know what column should contain what data, or if you only want to display a certain row (or rows). you can just make an IF statement checking whatever data, and skipping whatever doesn't fit your criteria. but, making those links the way you're doing it doesn't really help PHP at all (you're making a URL to "$count_row . $text[x]", which doesn't exist most likely). if you want to make that data available to your PHP script, you can pass it through the query string:
PHP Code:
<a href="?row=<?php echo $count_row; ?>">filter this row</a>
then, when you load that page, the variable $_GET['row'] will contain the data that $count_row held (for whichever row was clicked).
I'm not quite sure why you have that empty array ($grid_array_down); if all you need is access to the numbers 0 through 20, you can use $count_row. $count_row starts at 0, and will increment itself by 1 every row.
this is a slightly modified version of php.net's example on using fgetscsv(), which should do exactly what you're doing already:
PHP Code:
<?php
$row = 0;
$handle = fopen("library.csv", "r");
while(($data = fgetcsv($handle, 1024)) !== FALSE){
$row++;
for($i = 0; $i < count($data); $i++){
?>
<a href="<?php echo $row . $data[$i]; ?>">(<?php echo $row . $data[$i]; ?>)</a>
<?php
}
}
fclose($handle);
?>
assuming that you are trying to accomplish the same goal as you were originally, then we can use this all as a way of making the selected author's data show up only. like I've said before, there is no way for us to sort the data in any way using this method (by book name, author name, year, etc). we can only filter results. using the two examples above, we can do this:
PHP Code:
<pre>
<?php
//if $_GET['author'] is set in the query string, $author will = $_GET['author']
$author = (isset($_GET['author'])) ? $_GET['author'] : "";
//you can set $_GET['author'] by going to:
// <this_file>.php?author=<author_name>
// where <this_file> is the file name and <author_name> is the author.
$row = 0;
$handle = fopen("library.csv", "r");
while(($data = fgetcsv($handle, 1024)) !== FALSE){
//assuming $data[0] contains the author, we can do this:
if($author != "" && $data[0] != $author) continue;
//skip if $author is not empty and $author is not equal to the current author
$row++;
?>
<?php echo $row + 1; ?>. <a href="?author=<?php echo urlencode($data[0]); ?>"><?php echo $data[0]; ?></a>
<?php
//start from array index 1 (instead of 0) because author (0) was already displayed.
for($i = 1; $i < count($data); $i++){
echo "\t{$data[$i]}";
}
echo "\n"; //break the line.
}
fclose($handle);
?>
</pre>
I'm not sure if that's exactly what you wanted, but it might get you started. this will create a page that displays a bunch of lines, separated with tabs ("\t"), with $data[0] (I assumed to be the author) as a clickable link. when you click this link, it will filter the results to only show data from that author. if you want to filter the other columns, too, you can use the same method I used to filter the author.
and, last of all, a cron job/scheduled task won't take into account your coding skills. it would just run your script every X amount of time, and would take all of your CSV data and insert it into a database. I would highly recommend doing this if you have a large amount of data or would just like to be able to use/access that data better. this would make it much easier to search through, sort, and filter. if you'd like something like that, and have access to a MySQL database, let me know and I can show you how you might accomplish it!
hope that makes sense @_@.
also, from now on, please use [php] and [/php] around your code it make it easier to understand.
-
Mar 30th, 2009, 03:00 AM
#6
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
Thank you for the help, what I’m looking to achieve is to load-up a csv file - not only display the information but hide irrelevant information to the user.
The page shows one column 'Author'. The user clicks on an author (one is shown on the page but listed many times in the csv), then another column reads the csv and displays the books by the author. One is clicked and then the SKU code is given in the third column.
I have produced something similar in shockwave...
http://www.arborealservices.com/dir/advanced.dcr
...but I'm looking to make this more accessible, and learn Php.
If you think this is a lost cause or think I should be looking at something else any advice would be welcome.
Thanks
-
Mar 30th, 2009, 03:27 AM
#7
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
I know I can identify columns by $text[x] and I'm using $grid_array_down as an attempt to identify rows.
When testing, $stagelist tells me everything on the row I've selected.
My logic behind It all might be screwedup but so far it appears to be working, I'm just looking to identify/ unify repeated strings and hide (turn on/off) information listed that might not be relevent to the viewer.
possibly try using 'array_unique()' and 'array_search()'
I'm still getting my head around the code printed above (Thanks!), I'm sure I'll be able to use it or adapt mine is some way.
Last edited by LingoOutsider; Mar 30th, 2009 at 04:24 AM.
-
Mar 30th, 2009, 04:55 AM
#8
Re: multidimensional csv linking - no idea?!
if you haven't, you should load and run the code I posted. from what you've just stated, it sounds like what I've made should suit you for the most part. like I said in my previous post, "$grid_array_down" is completely useless, and you should disregard it. you don't have an array to refer to with any row numbers, and the $row variable will tell you the current row you're on. you don't need to use array_unique() or array_search() either; you don't have an array to search though.
if the first column in every row is the author, then $data[0] is the author. if you look at the code provided, I check if $author is equal to $data[0]; this should be changed to whatever column the author is stored in. actually, you would need to change all iterations of $data[0] in that script to fit whatever column the author is in (there are just a few). if you have any questions about the rest of the code (and what it is actually doing), then ask away.
Last edited by kows; Mar 30th, 2009 at 05:00 AM.
-
Mar 30th, 2009, 05:46 AM
#9
Thread Starter
Addicted Member
-
Mar 30th, 2009, 06:51 AM
#10
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
Is there an alternative to using array_unique() to remove repeated names (strings).
I'm thinking I need to split my csv from a multidimentional array ?? - and run array_unique() then display the list?
How can I put a column in to an array? I have (experimenting at some point) managed to split the columns into a groups but when i test the code by viewing the varible there in, they all come up as... array[0] "" array[0] "" array[0]. ""?
I know I'm probably going the wrong way but I can't think of away around?
Last edited by LingoOutsider; Mar 30th, 2009 at 09:36 AM.
-
Mar 30th, 2009, 03:35 PM
#11
Re: multidimensional csv linking - no idea?!
if you're trying things, you should post your code so that I can see what you might be doing wrong. still, you have no way of using array_unique(); you do not have an array to use it on :/ this is a very simple concept, so keep it that way 
if you want to put your csv into a multidimensional array, do something like this:
PHP Code:
<?php $books = array(); $handle = fopen("csv.csv", "r"); while(($data = fgetcsv($handle, 1024)) !== false){ //we save the ENTIRE row into the $books array, under the author's name $books[$data[0]][] = $data; } fclose($handle);
//now, sort books by the key (author) alphabetically. ksort($books);
$filterauthor = (isset($_GET['author'])) ? $_GET['author'] : "";
//print the books foreach($books as $author => $book){ //if the user has an author selected, skip this author if it doesn't match if($filterauthor != "" && $filterauthor != $author) continue; elseif($filterauthor != ""){ //print a link to go back ?> <a href="?">Go back</a> <hr /> <?php } ?> <h1><a href="?author=<?php echo $author; ?>"><?php echo $author; ?></a>:</h1> <?php //print each book for this author foreach($book as $data){ ?> <blockquote> <strong>Book name:</strong> <?php echo $data[1]; ?><br /> <strong>Year:</strong> <?php echo $data[2]; ?><br /> <strong>SKU:</strong> <?php echo $data[3]; ?><br /> <!-- keep adding these for the rest of your fields, and change which index $data refers to --> </blockquote> <?php } ?> <hr /> <?php } ?>
if you have a large number of books, you should take this one step further and use a database. otherwise, this will be a fairly slow solution, and still provides no real useful way to handle and sort the data.
edit: quick change.
Last edited by kows; Mar 30th, 2009 at 03:43 PM.
-
Apr 1st, 2009, 01:40 AM
#12
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
This is the code that I used to seperate the csv file into arrays...
PHP Code:
$file_handle = fopen("library.csv", "r"); //open the file
while (!feof($file_handle) ) { $text = fgetcsv($file_handle, 1024 ); //get info from file
$modelarray = array_slice($text,1,1); $model = array_unique($modelarray); print_r ($modelarray); } fclose($file_handle);
...when I use print_r command it shows the items in the first column but they all have the value of [0] so the array_unique doesn't have an effect
Last edited by LingoOutsider; Apr 1st, 2009 at 01:43 AM.
-
Apr 1st, 2009, 01:56 AM
#13
Re: multidimensional csv linking - no idea?!
I'm not sure -where- you're getting the ideas from to use array_slice() or array_unique(); but you need to understand that you do not HAVE an array in the first place to filter or slice. the WHILE loop you're using takes one line of the CSV file at a time and places it temporarily in the $text array. neither of those functions will benefit you. look at the documentation (www.php.net) for what both of those functions actually do.
-
Apr 1st, 2009, 03:00 AM
#14
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
Thank you for you help, the code that you have given certainly moves me in the right direction. The only part that doesn't match what I'm looking to do is hide the information in the column's further down the line and show the information (options) from previous columns.
columnA...columnB...columnC...columnD
opt.A.......opt.B1.....opt.B2(i)>opt.B2(i)a......(etc)
opt.B---->opt.B2-->opt.B2(ii)
opt.C.......opt.B3
opt.D
I think everything I'm looking for is there It's just a case of identifying, copying and re-organising the parts I'm after.
Last edited by LingoOutsider; Apr 1st, 2009 at 08:37 AM.
-
Apr 1st, 2009, 08:22 AM
#15
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
The only resources that I've look at are PHP for Dummies and w3schools PHP website do you know of any books worth looking at for the type of coding being used/needed?
(not so keen on Php net)
Last edited by LingoOutsider; Apr 1st, 2009 at 08:34 AM.
-
Apr 1st, 2009, 02:57 PM
#16
Re: multidimensional csv linking - no idea?!
no, I have no idea what books may have information about this. if you need any information on functions, I recommend the manual at PHP.net.
I also have no idea what you're trying to say about hiding information in the later columns, about options from previous columns? you're going to have to try explaining that better. I see your diagram, but I can't really make sense of what you actually want to happen.
-
Apr 1st, 2009, 03:51 PM
#17
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
Well I'm looking to have this as the out come displayed to the user;
columnA...columnB...columnC...columnD
opt.A.......opt.B1.....opt.B2(i)>opt.B2(i)a......(etc)
opt.B---->opt.B2-->opt.B2(ii)
opt.C.......opt.B3
opt.D
with information given from a csv file like...
columnA,columnB,columnC,columnD
opt.A,opt.A1,opt.A1(i),opt.A1(i)a
opt.B,opt.B1,opt.B2(i),opt.B2(i)a
opt.B,opt.B2,opt.B2(ii),opt.B2(i)a
opt.B,opt.B2,opt.B2(ii),opt.B2(ii)a
opt.B,opt.B3,opt.B3(i),opt.B3(i)a
opt.C,opt.C3,opt.C3(i),opt.C3(i)a
opt.D,opt.D1,opt.D1(i),opt.D1(i)a
...this is so if the viewer is looking for a book by auther 'opt.B' but it has been published by a number of publishers 'columncC' they can find 'opt.B2(i)a' and go back and look at the options under publisher 'opt.B2(ii).
I know It's quite confusing and can be achived by producing a silly amount of html files - but I'm hoping I can work out a way of doing it with PHP.
I just want people to be able to alter their previous selections on the page their viewing.
(the csv will be update but not that often its just wanted as a guide)
-
Apr 1st, 2009, 05:56 PM
#18
Re: multidimensional csv linking - no idea?!
well, if you're wanting to get into multiple filters like that, you really should look into using a database. this will give you much more flexibility. if you have access to a MySQL database, you can turn the top part of the script I provided into a tool to insert into the database, and then make a new script that will actually interact with the database. if your csv file will change, then you should run the script frequently or set up a scheduled task/cron job.
let me know if you have access to a database, and if so, if you have any experience with SQL. I can try to help you set it all up.
without a database, it's still possible to do what you want, it's just messy. because of the way I built the $books array, you can't filter by publisher; if the user wants to filter by publisher, you'd have to build the array using the publisher's name as the key. the only problem then is that it really makes it difficult to have multiple filters active at the same time. I'd rather not write a bunch of hacked up code to accomplish an easy task, when there are much better solutions ;)
Last edited by kows; Apr 1st, 2009 at 06:01 PM.
-
Apr 2nd, 2009, 03:14 PM
#19
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
The only database software I have is MS Access
I will attempt to get MySQL if you think its my best option. I haven't any real experience of database use out side excel, but as always any learning is good
I'm reasonable with Visual Basic, Directors Lingo, VRML and html.
I'd like to think I can do Basic PHP But some code does go over my head.
So far i'm just using EasyPHP, Excel and a text editor.
If you can help, or give advice it would be a great help, Thanks.
-
Apr 2nd, 2009, 04:23 PM
#20
Re: multidimensional csv linking - no idea?!
if you want to build any sort of practical application with this data that will be easy to maintain, using a database would definitely be the way to go. getting more in depth with multidimensional arrays generally gets people a lot more confused, however, using a database is fairly simple and doesn't require any of the hassle.
do you have some sort of hosting plan, or is this on a server of your own? most PHP hosts come coupled with MySQL. if it's your own box, then things can get tricky. installing it is fairly easy, but you probably want to google "installing php and mysql" for a quick tutorial. since it's your first time dealing with MySQL, you'll probably also want phpMyAdmin, and a tutorial for setting that up.
Last edited by kows; Apr 2nd, 2009 at 04:27 PM.
-
Apr 3rd, 2009, 12:22 AM
#21
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
I currently have a basic hosting account with godaddy, and looking at there advice notes it appears that I should be able to setup an MySQL Server database with phpMyAdmin.
I'll read-up more and look at a few SQL books.
Last edited by LingoOutsider; Apr 3rd, 2009 at 02:51 AM.
-
Apr 3rd, 2009, 05:39 AM
#22
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
My Godaddy account Gives me the option of using MySQL 4.1 or 5.0 and phpMyAdmin 2.11.9.4
I'm able to create a 'structure' and upload the CSV file.
-
Apr 3rd, 2009, 05:36 PM
#23
Re: multidimensional csv linking - no idea?!
that sounds great. you can use MySQL 5 to create a table (structure?) for your books. you will want to make an index for the table to make it easier for you to identify/search books. this will need to be a primary key and have an auto_increment on it.
not sure if you've done anything yet, but just in case you haven't, I'll show you how to create a table! first, your table structure should look something like this (assuming you only have author, title, publisher, and sku fields):
Code:
+----+--------+-------+-----------+-----+
| id | author | title | publisher | sku |
+----+--------+-------+-----------+-----+
you can create a table using phpMyAdmin using that as a model. ID should be an integer (int with a maximum length of 5 or so [up to 99,999 records], primary key with "auto_increment"), author should be a varchar (variable length string with a maximum length of 50-100 characters [be sure to give enough room for your longest author names]), title can also be a varchar (same as author, give enough room for the longest book titles), publisher can be a varchar as well, and sku can be a varchar or int (depending on if it carries only numbers, or numbers and letters, with a maximum length of whatever the longest sku could be).
you can look at and play with phpMyAdmin's create-a-table form to do this, or simply look at the SQL below:
Code:
CREATE TABLE `my_table_name` (
`id` int(5) unsigned NOT NULL auto_increment,
`author` varchar(100) NOT NULL,
`title` varchar(100) NOT NULL,
`publisher` varchar(100) NOT NULL,
`sku` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
the names involved (which can be changed) are in bold. you should change the table's name at the very least, and use the structure of the query above to add more fields if necessary. you can copy and paste this query and go to the "SQL" tab in phpMyAdmin to run it.
when you're finished creating your table, you can go to the "Insert" tab to insert your data if you'd like, or you can learn how to write a script to do that for you. I'm not sure if you can just upload your csv file and have it work, though.
now, once you've created a table, you'll need to learn how queries work. for a basic run down, you can take a look at this post that I made. it should also give you a very basic introduction on how to use MySQL with PHP. feel free to ask any questions.
-
Apr 4th, 2009, 05:04 AM
#24
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
Thanks I'll see how far I get.
-
Apr 6th, 2009, 01:34 AM
#25
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
So I can use GROUP BY to remove repeated 'strings' in a column. I'm unsure with the linking and recording user imputs, the way I was doing it and your way doesn't want to work.
Any more advice or tutorials I can look at?
Thanks
(The code show in the example didn't work for me I had to change 'mysql_query("SELECT * `tablename`")' to 'mysql_query("SELECT * FROM `tablename`")')
Last edited by LingoOutsider; Apr 6th, 2009 at 06:55 AM.
-
Apr 6th, 2009, 04:13 AM
#26
Re: multidimensional csv linking - no idea?!
you aren't any closer to where you were before.. at least not yet. you're actually further away from the goal than you were before, but it's faster to catch up when dealing with databases than with arrays. and, it's nearly impossible to efficiently sort/extract data from arrays like you are trying to do. this is what databases are best for!
if you've read everything over, then you should start applying it. this is an example, to display all of the books in the database:
PHP Code:
<?php //connect to our database server and select our database mysql_connect("localhost", "username", "password"); mysql_select_db("my_database");
//our SQL //we sort by author $sql = "SELECT * FROM books ORDER BY author ASC";
//send it to mysql $query = mysql_query($sql);
//keep track of which author we are on $last_author = "";
//display the results, one by one while($books = mysql_fetch_assoc($query)){
//only display the author name if we're not already displaying his books ?> <?php if($last_author != $books['author']): ?><hr /><h1><?php echo $books['author']; ?></h1><?php endif; ?> <blockquote> <h2><?php echo $books['title']; ?></h2> Published by <em><?php echo $books['publisher']; ?></em> </blockquote> <?php //set the last author to this author to keep track $last_author = $books['author']; } ?> <hr />
this script will do the same thing that one of the original scripts does. it displays each author, and each book by that author. you can use the WHERE clause in a query to change what you're looking for. using past code as an example, if you made a link that added the query string "?author=<authorname>", we could change $sql to:
Code:
SELECT * FROM books WHERE author='<authorname>' ORDER BY author ASC
we can then do this dynamically by checking if "author" is set in the $_GET array, and if it is, append the $sql variable:
PHP Code:
$sql = "SELECT * FROM books "; $sql .= (isset($_GET['author'])) ? " WHERE author='" . mysql_real_escape_string($_GET['author']) . "' " : ""; $sql .= " ORDER BY author ASC";
you could also do something similar that would filter by publisher. hopefully the logic makes sense to you. play around with it, and ask questions if you're confused about how to do something. this may not be exactly what you want to do, but it gives you a lot of flexibility to do whatever you'd like. when you do have a question, be specific and make sure you're posting what you've been trying.
-
Apr 6th, 2009, 08:40 AM
#27
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
So far I have...
PHP Code:
<?php //connecting to mysql_connect("hostedresource.com", "Library", "password") or die("could not connect to MySQL server"); //selecting database mysql_select_db("ChainLibrary") or die("could not select MySQL database"); ?>
to load the Information, and the following to paste into a field/table...
PHP Code:
<?php
// Make a MySQL Connection
$query = "SELECT * FROM Demo GROUP BY id"; $result = mysql_query($query) or die(mysql_error());
// Print out result while($row = mysql_fetch_array($result)){ echo " - "; echo "<a href=\"?page=$row[columnA]\"> <strong>$row[columnA]</strong> </a>" ; }
?>
How/Can I use mysql_fetch_row() to recive set information like it appears you have?
And while I can change the 'page' in the URL I'm not sure how to record and search for the rows that have that Information in?
(trying to limit the results to only relevent ones from the users selection)
Last edited by LingoOutsider; Apr 6th, 2009 at 09:22 AM.
-
Apr 7th, 2009, 12:56 AM
#28
Re: multidimensional csv linking - no idea?!
to retrieve specific information, you use the where clause. in my code, the important points to look at are what I'm sending to the database (eg. what $sql is defined as). at the end of my post I tried to make an example for you. I'll break it down for you, if that will make things easier. first, the basic query is made:
SELECT * FROM books
then, we check if a "modifier" might be set. in my example, we check if $_GET['author'] exists, and if it does, that means the user must have clicked on a link. in your code, you're not specific (you define "columnA" as a "page"), though I'm not sure if you changed that for the post or not. you can't really just define it as "page" (well, you can, but it's just not very informative). you could name it "columnA," though, if you want. so, I'm checking if $_GET['author'] is set to anything, and if it is, then we add onto the query to make it more specific. it now becomes:
SELECT * FROM books WHERE author='{$_GET['author']}'
then, we need to finish up our query and tell it what to sort by. we can also sort by multiple rows if we need to, which would be good in the case that we're only showing one author. our final query will look something like this:
SELECT * FROM books WHERE author='{$_GET['author']}' ORDER BY author ASC, columnB DESC, columnC ASC
of course, you don't need to include columnB/columnC in your own query. so, what will this query do? english translation: select all fields from the table books where the author of the book is $_GET['author'], and order it first ascending by author, then descending by columnB, then ascending by columnC. all of that put into code is:
PHP Code:
$sql = "SELECT * FROM books "; $sql .= (isset($_GET['author'])) ? " WHERE author='" . mysql_real_escape_string($_GET['author']) . "' " : ""; $sql .= " ORDER BY author ASC";
basically, if you want to filter by publisher, you'll have to do this for the publisher, too. that's really just taking the code you've already made and replicating it and applying it to something else, though.
also, you should never trust your user's input. $_GET['author'] could contain something that would break your script, or assault your database. in this case, it's probably harmless, but you should always make your SQL queries "safe" by using a function like mysql_real_escape_string(). you can define a variable that you will specifically use in your queries when dealing with user input:
PHP Code:
$safe_author = mysql_real_escape_string($_GET['author']); $sql = "SELECT * FROM books WHERE author='$safe_author'";
and, from looking at what you've done so far, you're using the SQL wrong ;) not to worry, your query didn't do anything wrong, but you're using the "GROUP BY" function the wrong way. this is a function that will group together a bunch of similar table rows based on the field passed to it (id, in this case). because all of your IDs are unique, the function really doesn't do anything. I'm not sure if you just mean to type "order by" or not, though.
Last edited by kows; Apr 7th, 2009 at 01:02 AM.
-
Apr 7th, 2009, 03:02 AM
#29
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
Thanks for your help,
SELECT * FROM WHERE makes sence to me .
I'm using GROUP BY to remove repeted names in the columns.
I can certainly see see the outcome I'm after
Thanks again.
Last edited by LingoOutsider; Apr 7th, 2009 at 03:57 AM.
-
Apr 7th, 2009, 04:06 AM
#30
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
For the script that gets repeated depending on what information I'm after...
PHP Code:
<?PHP $query = "SELECT * FROM Demo WHERE book='{$_GET['auth']}' GROUP BY Publisher"; $result = mysql_query($query) or die(mysql_error());
// Print out result while($row = mysql_fetch_array($result)){ echo "<a href=\"?Auth=$row[Publisher]\"> <strong>$row[Publisher]</strong> </a><BR>"; } ?>
It works as far as I can move from one column to the next, but I loose the view of previous choices. Is there a way to have multiple 'Auth', 'Mod', 'etc' in the URL link or is the best/only way to try and store the information in a variable?
Last edited by LingoOutsider; Apr 7th, 2009 at 05:06 AM.
-
Apr 7th, 2009, 07:11 AM
#31
Re: multidimensional csv linking - no idea?!
yes, you can absolutely have as many variables in your URL as you could possibly want. you just need to be careful about how you're beginning to handle having multiple variables! if you used a method like I did before and added the "WHERE" for author, you might end up with a query that looked like:
SELECT * FROM books WHERE author='blah' WHERE publisher='blah' ...
which would be invalid. you'd need to first check if any of those values were set, and then add onto your where statement. you could try:
PHP Code:
$sql = "SELECT * FROM books "; if(isset($_GET['Auth']) || isset($_GET['Publisher'])){ //one of these is set, so we automatically append our $sql $sql .= "WHERE "; $i = 0; //keep track of if we've added to query if(isset($_GET['Auth'])){ $sql .= " author='{$_GET['Auth']}' "; $i++; } if(isset($_GET['Publisher'])){ if($i > 0) $sql .= " AND "; //add an AND if we've added to sql previously $sql .= " publisher='{$_GET['Publisher']}' "; $i++; } } $sql .= " ORDER BY author ASC";
so, if $_GET['Auth'] and $_GET['Publisher'] were both set, our query would be:
SELECT * FROM books WHERE author='{$_GET['Auth']}' AND publisher='{$_GET['Publisher']}' ORDER BY author ASC
or, if only publisher was set:
SELECT * FROM books WHERE publisher='{$_GET['Publisher']}' ORDER BY author ASC
hope that makes sense.
-
Apr 7th, 2009, 03:24 PM
#32
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
Everything works and looks good I'm getting an error because the variable I'm using to store the URL information doesn't always have a value. I can get around this by linking straight to the '?auth=' page but I understand there is some code out there to turn off the error reporting?
Thanks for your help kows I don't think I would have got there without you
-
Apr 7th, 2009, 03:38 PM
#33
Re: multidimensional csv linking - no idea?!
you could turn off error reporting by calling this at the top of your script:
PHP Code:
error_reporting(0);
but, if you're dealing with variables that aren't set, I suggest you just make a call to make sure they exist before you use them. this is why I use the isset() function so much.
PHP Code:
$myvar = (isset($_GET['myvar'])) ? $_GET['myvar'] : '';
the above code will set $myvar to $_GET['myvar'] if that variable is set, otherwise it's set to an empty string. you could either then check if $myvar is empty before doing anything, or just use isset() on $_GET['myvar'] before using it.
-
Apr 15th, 2009, 06:46 AM
#34
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
PHP Code:
$query = "SELECT * FROM database WHERE booktitle='{$ManuRecord[2]}' GROUP BY SKU ORDER BY id";
$result = mysql_query($query) or die(mysql_error());
echo "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"2\" bgcolor=\"#000000\" id=\"table\">";
$standard = 0;
$count_color = 0;
// Print out result
while($row = mysql_fetch_array($result)){;
echo "<tr>";
$standard = $standard + 1; //counter to identify 1st item in list
$count_color = $count_color + 1; //counter to compair with ($ManuRecord) URL ref.
if (($count_color != $ManuRecord[13]) && ($standard < 2))
echo "<td align=\"center\" bgcolor=\"#FFFFFF\"><a href=\"?Manu=$row[Auther]:$row[booktitle]:...:$row[SKU]$count_color\"><span style=\"color:orange\"><strong>$row[publisher]</strong></span></a></td>";
else if ($count_color != $ManuRecord[13])
echo "<td align=\"center\" bgcolor=\"#FFFFFF\"><a href=\"?Manu=$row[Auther]:$row[booktitle]:...:$row[SKU]$count_color\"><span style=\"color:black\"><strong>$row[publisher]</strong></span></a></td>";
else
echo "<td align=\"center\" bgcolor=\"#FFFFFF\"><a href=\"?Manu=$row[Auther]:$row[booktitle]:...:$row[SKU]$count_color\"><span style=\"color:red\"><strong>$row[publisher]</strong></span></a></td>";
echo "</tr>";
}
echo "</table>"
I'm now looking to highlight selected information. So far with the code i have I've managed to highlight previous selected options in red. have the item at the top of the list (the 1st/main option) in orange and everything else in black.
I can't get the orange item to change to black when something else in the list of results is clicked.
Messy code yes!? Any advice is great Thanks!
note: $ManuRecord is the information in the URL (list of clicked on links) stored in an array.
-
Apr 15th, 2009, 10:57 PM
#35
Re: multidimensional csv linking - no idea?!
you are going to have to be much, much more informative with your code, because I don't understand what's going on. what is $ManuRecord[13]? why are you setting up your URL like you are? it seems like you're not taking full advantage of the query string. I'll try to give you a rundown on how it works!
Assuming your query string is as follows (using your stuff as an example):
?author=David&booktitle=This%20is%20My%20Book&sku=3023013&color=2
then PHP will have the following variables available to you:
$_GET['author'] = David
$_GET['booktitle'] = This%20is%20My%20Book
$_GET['sku'] = 3023013
$_GET['color'] = 2
all of this information is stored in the URL (and the %20s in the booktitle are just URL encoded spaces, you don't need to worry about that) and doesn't require any special parsing, like it looks like you are doing with all of the colons in your URL.
few more tips
you can increment integers like this:
PHP Code:
$standard++; $count_color++;
but, because you are ALWAYS incrementing $standard AND $count_color, you could easily use just one variable to account for both.
you're echoing the SAME line three times. instead, echo ONE line and use variables to change the things that will be changing. like so:
PHP Code:
if(($count_color != $ManuRecord[13]) && ($standard < 2)){ $color = "orange"; }elseif($count_color != $ManuRecord[13]){ $color = "black"; }else{ $color = "red"; } echo "<td align=\"center\" bgcolor=\"#FFFFFF\"><a href=\"?Manu=$row[Auther]:$row[booktitle]:...:$row[SKU]$count_color\"><span style=\"color:$color\"><strong>$row[publisher]</strong></span></a></td>";
and, last but not least.. PHP is a language that is EMBEDDED into HTML. you should try your best to NEVER actually be using echo to print HTML. instead, embed it:
PHP Code:
<?php $title = "My Page"; $header = "Welcome!"; $body = "Hello world!"; ?> <html> <head> <title><?php echo $title; ?></title> </head> <body> <h1><?php echo $title . " - " . $header; ?></h1> <blockquote> <p><?php echo $body; ?></p> </blockquote> </body> <html>
This makes everything much easier to read, and eliminates ALL debugging problems associated with an un-escaped quote, for example. Because the PHP parser doesn't need to "read" the HTML, it also might be running faster. basically, your code might change to something like this:
PHP Code:
<table width="100%" border="0" cellpadding="0" cellspacing="2" bgcolor="#000" id="table"> <?php $standard = 0; $count_color = 0; while($row = mysql_fetch_array($result)){ $standard++; $count_color++; $color = "orange"; //removed logic to make my life easier ?> <tr> <td align="center" bgcolor="#fff"> <a href="?Manu=<?php echo $row['Author']; ?>&booktitle=<?php echo $row['booktitle']; ?>&sku=<?php echo $row['SKU']; ?>&color=<?php echo $count_color; ?>"><span style="color: <?php echo $color; ?>"><strong><?php echo $row['publisher']; ?></strong></span></a> </td> </tr> <?php } ?> </table>
and, you might also want to look into using a style sheet to more easily style your links and tables to cut down on your HTML :)
HTML Code:
<head>
<style type="text/css">
table { border: 0; background: #000; border-collapse: collapse; }
td { background: #fff; text-align: center; }
td a { font-weight: bold; }
a.red { color: #ff0000; }
a.orange { color: #ff8000; }
a.black { color: #000; }
</style>
</head>
<body>
<table cellspacing="2">
<tr>
<td><a class="red" href="?manu=blah">This will be red</a></td>
<td><a class="orange" href="?manu=blah">This will be orange</a></td>
<td><a class="black" href="?manu=blah">This will be black</a></td>
</tr>
</table>
</body>
this should style your links the same way that you were before, but using a style sheet instead. this makes things much easier to edit in the long run, too. you'd mostly be insterested in the stuff in the <style> tag and then the <table> tags.
hope that all made sense! a lot of information in one post, I'm sure :x aside from all of the tips, ask/post more information about your problem, too! it seems like it would be simple to solve, I'm just not sure how you've got everything set up.
Last edited by kows; Apr 15th, 2009 at 11:00 PM.
-
Apr 17th, 2009, 02:53 AM
#36
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
I'm using this code to identify what choices the user has selected...
PHP Code:
$mousedown = $_GET["Manu"]; //record the link pressed
$ManuRecord = explode(":",$mousedown); // chop up into an array
...so $ManuRecord[13] will be the 13th Item selected in the URL.
(URL just recording chosen options from SQL database)
I'll try sorting out the code and use an array for the colours, I hope it works!
Thanks.
-
Apr 20th, 2009, 06:10 AM
#37
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
Thanks for your help, I'm pretty much there.
Its not vital but is it possible to put each section of Php code...
(the one that identiys the auther, the one that identifys the book, etc)
...into seperate frames without interfearing with the URL
(manu=auther:etc)
PHP Code:
so it an appears in a scrollable box like these?
-
Apr 20th, 2009, 12:56 PM
#38
Re: multidimensional csv linking - no idea?!
I guess you could use an iframe, but those are generally frowned upon. thispage.php would be stored in an iframe, which would be independent of the original page (in an iframe).
index.php:
HTML Code:
<iframe name="something" src="thispage.php" width="400" height="400"></iframe>
<iframe name="somethingelse" src="otherpage.php" width="400" height="400"></iframe>
<a href="http://google.com" target="something">Open Google in iframe1</a>
<a href="http://vbforums.com" target="somethingelse">Open VBForums in iframe2</a>
you can rename the iframes as necessary, and if you ever want to open a page in one specific iframe, just make sure that you set the "target" attribute in the link.
I'm not totally sure what you're trying to do with it though, so I can't really think of any alternate methods you might want to look at.
-
Apr 21st, 2009, 04:55 AM
#39
Thread Starter
Addicted Member
Re: multidimensional csv linking - no idea?!
iframe looks to be what im after thanks.
the information i have in the frame uses...
PHP Code:
$mousedown = $_GET["Manu"]; //recorded links in URL
$ManuRecord = explode(":",$mousedown); // chop up into an array
I doesn't look like it wants to read the URL information. Is there such a thing as a scrolling table because the information doesn't have to appear on a different page.
The php at the moment shows the information from the SQL database in a table/list If more items are added the page could become stupidly large! I'm looking to confine each part in their own scrollable box/table.
-
Apr 21st, 2009, 02:20 PM
#40
Re: multidimensional csv linking - no idea?!
well, if you don't want to separate pages you can just enclose the information in a <div>, and make it scroll.
HTML Code:
<div style="height: 400px; width: 200px; overflow: scroll;">
<!-- your content here -->
</div>
be sure to adjust the height/width to whatever you'd prefer.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|