PHP - Putting Array Contents Into Html Table
Hi Everyone,
I have a problem displaying some information from an array (selected from my database). The array is below. Array ( [0] => Array ( [ssc_skill_categories] => Web [sc_skill_categories] => Programming ) [1] => Array ( [ssc_skill_categories] => Actionscript [sc_skill_categories] => Programming ) [2] => Array ( [ssc_skill_categories] => C# [sc_skill_categories] => Programming ) [3] => Array ( [ssc_skill_categories] => CSS [sc_skill_categories] => Programming ) [4] => Array ( [ssc_skill_categories] => Graphic [sc_skill_categories] => Designers ) [5] => Array ( [ssc_skill_categories] => Logo [sc_skill_categories] => Designers ) [6] => Array ( [ssc_skill_categories] => Illistration [sc_skill_categories] => Designers ) [7] => Array ( [ssc_skill_categories] => Animation [sc_skill_categories] => Designers ) ) What i would like to to is display this information in a table like so: <html> <body> <table> <tr> <td>Programming</td><td>Web</td><td>Actionscript</td><td>C#</td><td>CSS</td> <tr> <tr> <td>Designers</td><td>Graphic</td><td>Logo</td><td>Illistration</td><td>Animation</td> <tr> <table> </body> </html> I have been trying and failing all day to do this. Posting my "progress" will clog up the thread, so for now i wont post it. Does anyone have an idea how i would achieve this? Regards, -Ben Similar Tutorials//First I'm assigning a $variable ($emailzipmatch) to query a database table called(repzipcodes) and having it pull and display 1 to 3 records based on matching up a customer's zip code (RepZipCode = $CustomerZipMatch) with 1 to 3 other people (GROUP BY RepId HAVING COUNT(1) <= 3") that want that customer's information from that particular zip code. <This code works fine> // CODE WORKS BELOW Code: [Select] <?php $emailzipmatch = mysql_query("SELECT * FROM repzipcodes WHERE RepZipCode = $CustomerZipMatch GROUP BY RepId HAVING COUNT(1) <= 3") or die(mysql_error()); $recipients = array(); while($row = mysql_fetch_array($emailzipmatch)) { $recipients[] = $row['RepEmail']; echo "Agent's Email Address: "; echo 'font color="#FF7600"',$row['RepEmail'], '/font'; echo '<br />'; echo "Rep's ID: "; echo '<br />'; echo 'font color="#FF7600"',$row['RepId'], '/font'; echo '<br />'; echo 'hr align="left" width="50%" size="2" /'; } //MY PROBLEM BELOW // For the NEXT step of the process above I would take $row['RepEmail'] and $row['RepId'] which can have 1 to 3 results and assign the 1 to 3 results a new $variable so it can be inserted into a different db table so I can track the results of the query ($emailzipmatch = ) from the top of the page: ie.. <New Variable> <Listed from above> $SentRepId 0 = RepId (results from above echo area) $SentRepId 1 = RepId (results from above echo area) $SentRepId 2 = RepId (results from above echo area) // Below I'd like to insert the above results into a new database $?Variable??? = mysql_query("INSERT INTO sentemail (SentRepId0, SentRepId1, SentRepId2,SentDateTime ) VALUES ( '$_SESSION[RepId]', // ????? '$_SESSION[RepId]', // ????? '$_SESSION[RepId]', // ????? NOW() )") or die(mysql_error()); Thank ahead of time for any help you guys can give me. Please respond with ANY question if my coding or request isn't clear or if I've been confusing due to my lack of experience with PHP and MySQL. MOD EDIT: code tags added. Hello all, I'm sure something like this has been brought up before but I think it's quite a unique problem and I didn't get anywhere searching... so here's my first post. Yay. Right, so, basically I have a database table that looks like this: Code: [Select] +----------+-------+ | name | value | +----------+-------+ | settingA | true | | settingB | false | | settingC | false | +----------+-------+ And I have been racking my brains trying to figure out how to get that data into an array formatted like so: Code: [Select] Array ( [settingA] => true [settingB] => false [settingC] => false ) The best I have been able to do, using nested foreach() statements, is this: Code: [Select] Array ( [0] => Array ( [name] => settingA [value] => true ) [1] => Array ( [name] => settingB [value] => false ) [2] => Array ( [name] => settingC [value] => false ) ) ...which is not very practical, or at least it isn't for what I want to do. Any ideas on what the best method might be for achieving this? I am using ADOdb Lite but even an example using PHP's native MySQL functions could help me figure out what to do. Thanks! Hi again all, Why does the foreach loop im doing, put the array values from collection, into seperate table rows rather than 1 row per whole array? <?php class registration{ public $fields = array("username", "email", "password"); public $data = array(); public $table = "users"; public $dateTime = ""; public $datePos = 0; public $dateEntryName = "date"; public $connection; function timeStamp(){ return($this->dateTime = date("Y-m-d H:i:s")); } function insertRow($collection){ //HERE foreach($this->fields as $row => $value){ mysql_query("INSERT INTO $this->table ($value) VALUES ('$collection[$row]')"); } mysql_close($this->connection->connectData); } function validateFields(){ $this->connection = new connection(); $this->connection->connect(); foreach($this->fields as $key => $value){ array_push($this->data, $_POST[$this->fields[$key]]); } $this->dateTime = $this->timeStamp(); array_unshift($this->data, $this->dateTime); array_unshift($this->fields, $this->dateEntryName); foreach($this->data as $value){ echo "$value"; } $this->insertRow($this->data); } } $registration = new registration(); $registration->validateFields(); ?> I end up with 3 rows row 1: username row 2: email row 3 : password rather than row 1:username email password. This is what i have.. I basicly need to take the Make and Model And Year Not the TYPE that needs to be left out and combine them into a drop down list. So example below would be like this Yamaha WR450F 2003-2006 Yamaha YZ450F 2003-2005 Code: [Select] <table> <tr> <th>Type</th> <th>Make</th> <th>Model</th> <th class='tar'>Year</th> </tr> <tr class='even'> <td>Offroad</td> <td>Yamaha</td> <td>WR450F</td> <td class='tar'>2003-2006</td> </tr> <tr class='odd'> <td>Offroad</td> <td>Yamaha</td> <td>YZ450F</td> <td class='tar'>2003-2005</td> </tr> </table> Hey Guys, I want to be able to upload a large amount of mp3's to a private area on our website and have a script that will scan the folder for all the filenames and then create a record in the db of all the filenames. It is to help with creating the playlist for my friends radio show. To manually insert all the filenames each month woudl be a nightmare! I have been messing with a partial script I found on the web, and it kind of does something but not what I want it to. heres the db id int(4) title varchar(250) mp3 varchar(250) sort int(4) visible int(1) added datetime heres the script i am trying to use: <? $listDir = array(); $dir = "./music"; if($handler = opendir($dir)) { while (($sub = readdir($handler)) !== FALSE) { if ($sub != "." && $sub != ".." && $sub != "Thumb.db") { if(is_file($dir."/".$sub)) { $listDir[] = $sub; }elseif(is_dir($dir."/".$sub)){ $listDir[$sub] = $this->ReadFolderDirectory($dir."/".$sub); } } } closedir($handler); } $db = mysql_connect("localhost","gfab_ml","xxxxxxx"); mysql_select_db("gfab_ml",$db); $colors=serialize($listDir); //takes the data from a post $sql="INSERT INTO ml_music (ID, title, added) VALUES('','$colors','')"; $result = mysql_query($sql) or die (mysql_error()); ?> it does insert the values into the db but what it does is creates 1 record and spits all the results in a really weird way like this. into the title field. a:18:{i:0;s:23:"14 Mr Magic Premier.mp3";i:1;s:23:"track1.mp3";i:2;s:15:"track2.jpg";i:3;s:15:"track3.fxp";i:4;s:12:"track4.mp3";i:5;s:7:"track5.mp3";i:6;s:7:"track6.jpg";i:7;s:7:"track7.mp3";i:8;s:12:"track8.mp3" Could anyone please help me to make it work? im a bit of a noob. but i do like to tinker! for starters, i am a complete beginner at PHP, so i know almost nothing.
so, the basic outline is that i am required to create a table with 2 columns, and use PHP where i can
so the first set of code lays down the table and the ability to type in text of "name" and "surname". ----i think this "input" is HTML, yes?? is there a way to enter it as PHP?
<table border=\"2\"> <tr><th><b>Requirements</b></th><th><b>Selections</b></th></tr> <tr><td>Name</td><td><input type="text" name="name"/></td></tr> <tr><td>Surname</td><td><input type="text" name="surname"/></td></tr>so, for this next section, adding on is the part for "age". i want this to appear as a dropdown selection. i've written PHP where possible, however this does not work when opening the file in a browser. it simply leaves a blank dropdown menu with no options. ???? <tr><td>Age</td><td><select> <?php for ($num=11; $num<=22; $num++){ echo '<option>' .$num. '</option>'; } ?> </select>lastly, i have the part for "activity choice". this again i believe i wrote the radio buttons in HTML??? am i able to write this as PHP???? <tr><td>Activity Choice</td><td><input type="radio" name="activityChoice" value "music"/> Music ($30.00)<br> <input type="radio" name="activityChoice" value "swimming"/> Swimming ($25.50)<br> <input type="radio" name="activityChoice" value "tennis"/> Tennis ($20.00)<br> <input type="radio" name="activityChoice" value "basketball"/> Basketball ($15.50)<br> <input type="radio" name="activityChoice" value "netball"/> Netball ($15.50)<br> <input type="radio" name="activityChoice" value "dance"/> Dance ($10.50)<br> <input type="radio" name="activityChoice" value "communityService"/> Community Service (No Charge)</td></tr> Hello everyone, Sorry if this has been answered but if it has I can't find it anywhere. So, from the begining then. Lets say I had a member table and in it I wanted to store what their top 3 interests are. Their$ row has all the usual things to identify them userID and password etc.. and I had a further 3 columns which were labled top3_1 top3_2 & top3_3 to put each of their interests in from a post form. If instead I wanted to store this data as a PHP Array instead (using 1 column instead of 3) is there a way to store it as readable data when you open the PHPmyadmin? At the moment all it says is array and when I call it back to the browser (say on a page where they could review and update their interests) it displays 'a' as top3_01 'r' as top3_02 and 'r' as top3_03 (in each putting what would be 'array' as it appears in the table if there were 5 results. Does anyone know what I mean? For example - If we had a form which collected the top 3 interests to put in a table called users, Code: [Select] <form action="back_to_same_page_for_processing.php" method="post" enctype="multipart/form-data"> <input name="top3_01" type="text" value="enter interest number 1 here" /> <input name="top3_02" type="text" value="enter interest number 2 here" /> <input name="top3_03" type="text" value="enter interest number 3 here" /> <input type="submit" name="update_button" value=" Save and Update! " /> </form> // If my quick code example for this form is not correct dont worry its not the point im getting at :) And they put 'bowling' in top3_01, 'running' in top3_02 and 'diving' in top3_03 and we catch that on the same page with some PHP at the top --> Code: [Select] if (isset($_POST)['update_button']) { $top3_01 = $_POST['top3_01']; // i.e, 'bowling' changing POST vars to local vars $top3_02 = $_POST['top3_02']; // i.e, 'running' $top3_03 = $_POST['top3_03']; // i.e, 'diving' With me so far? If I had a table which had 3 columns (1 for each interest) I could put something like - Code: [Select] include('connect_msql.php'); mysql_query("Select * FROM users WHERE id='$id' AND blah blah blah"); mysql_query("UPDATE users SET top3_01='$top3_01', top3_02='$top3_02', top3_03='$top3_03' WHERE id='$id'"); And hopefully if ive got it right, it will put them each in their own little column. Easy enough huh? But heres the thing, I want to put all these into an array to be stored in the 1 column (say called 'top3') and whats more have them clearly readable in PHPmyadmin and editable from there yet still be able to be called back an rendered on page when requested. Continuing the example then, assuming ive changed the table for the 'top3' column instead of individual colums, I could put something like this - Code: [Select] if (isset($_POST)['update_button']) { $top3_01 = $_POST['top3_01']; // i.e, 'bowling' changing POST vars to local vars $top3_02 = $_POST['top3_02']; // i.e, 'running' $top3_03 = $_POST['top3_03']; // i.e, 'diving' $top3_array = array($top3_01,$top3_02,$top3_03); include('connect_msql.php'); mysql_query("UPDATE members SET top3='$top3_array' WHERE id='$id' AND blah blah blah"); But it will appear in the column as 'Array' and when its called for using a query it will render the literal string. a r r in each field instead. Now I know you can use the 'serialize()' & 'unserialize()' funtcions but it makes the entry in the database practically unreadable. Is there a way to make it readable and editable without having to create a content management system? If so please let me know and I'll be your friend forever, lol, ok maybe not but I'd really appreciate the help anyways. The other thing is, If you can do this or something like it, how am I to add entries to that array to go back into the data base? I hope ive explained myself enough here, but if not say so and I'll have another go. Thanks very much people, L-PLate (P.s if I sort this out on my own ill post it all here) Thanks to the great help from some great people on this message board I'm right at the finish line on this page. I'm trying to mesh all the coding together at this point, and make it display the information. I keep getting 'mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource....etc..(filename/location) on line 51. Am I way off here??? Code: [Select] <?php $db_host = 'localhost'; $db_user = 'root'; $db_pass = ''; $db_in = new MySQLi ( $db_host, $db_user, $db_pass, 'sign_in' ); $db_out = new MySQLi( $db_host, $db_user, $db_pass, 'sign_out' ); if( $db_in->connect_error || $db_out->connect_error ) trigger_error( 'Unable to initiate database connections', E_USER_ERROR ); $q_in = 'SELECT * FROM `customer sign-in`'; $q_out = 'SELECT * FROM `customer sign-out`'; if( ($r_in = $db_in->query($q_in)) === FALSE || ($r_out = $db_out->query($q_out)) === FALSE ) trigger_error( 'Unable to grab ticket information from databases', E_USER_ERROR ); $data_in = array(); $data_out = array(); while( $row = $r_in->fetch_assoc() ) $data_in[ $row['Ticket #'] ] = $row; $r_in->free(); while( $row = $r_out->fetch_assoc() ) $data_out[ $row['Ticket #'] ] = $row; $r_out->free(); $result = array_diff_key( $data_in, $data_out ); echo "<table border='1'> <tr> <th>ID</th> <th>Sign In Date</th> <th>RANK/CIV</th> <th>First Name</th> <th>Last Name</th> <th>Unit</th> <th>DSN/Roshan</th> <th>Classifications</th> <th>Services Requested</th> <th>Service Tag/ Serial Number</th> <th>Ticket #</th> <th>Make/ Model</th> </tr>"; while($row = mysql_fetch_assoc($result)) { // Retrieve data until no more { echo "<tr>"; echo "<td>" . $row['ID'] . "</td>"; echo "<td>" . $row['Sign in Date'] . "</td>"; echo "<td>" . $row['Rank/CIV'] . "</td>"; echo "<td>" . $row['First Name'] . "</td>"; echo "<td>" . $row['Last Name'] . "</td>"; echo "<td>" . $row['Unit'] . "</td>"; echo "<td>" . $row['DSN/Roshan'] . "</td>"; echo "<td>" . $row['Classifications'] . "</td>"; echo "<td>" . $row['Services Requested'] . "</td>"; echo "<td>" . $row['Service Tag/ Serial Number'] . "</td>"; echo "<td>" . $row['Ticket #'] . "</td>"; echo "<td>" . $row['Make/ Model'] . "</td>"; echo "</tr>"; } } ?> Hi guys, I've got quite a few fields in my tables that i've serialised to keep the number of fields down. For everything else that works perfect as it stores the data and when needed I can use the following as an example: $dateofbirth = unserialize($row['dateofbirth']); $dobday = $dateofbirth[0]; $dobmonth = $dateofbirth[1]; $dobyear = $dateofbirth[2]; Date of birth is stored as dd,mm,yyyy and for everything else I can call it fine. My issue is now that i'm trying to use fputcsv to create a csv file using the following: $result = mysqli_query($con, 'SELECT u.user_id, b.dateofbirth FROM Users u INNER JOIN Basic b USING (user_id) ORDER BY user_id DESC'); $fp = fopen('latest.csv', 'w'); fputcsv($fp, array('User ID', 'DOB' )); The CSV generates, but for the date of birth column in the csv it outputs as "a:3:{i:0;s:2:"03";i:1;s:2:"02";i:2;s:4:"1986";}" because it's obviously still serialised. What is my best and or easiest way of handling these fields? Many thanks in advance. Edited by Kristoff1875, 21 January 2015 - 05:24 AM. I have a table with x ammount of rows and x ammount of columns, in each TD there is an input box which the user fills in, when they press save the information is input into the database in this form at 6,1,2,3,4,5,6,A,1,2,3,4,5,6,B,1,2,3,4,5,6 - the first number is the number of columns, 1- 6 is the top line of TDs (column headers) and then A 1-6 is row 1, B 1-6 is row 2 etc.. so i then out put the information into a table via a for loop : Code: [Select] while($row = mysql_fetch_array($r)) { $data = explode(',',$row["content_bottom"]); $datacount = count($data); echo"<table border='1'><tr><td>Tool Type</td>"; $k = 1; for ($i = 1; $i <= $datacount; $i++) { if($data[$i] !=""){ echo "<td>".$data[$i]."</td>"; if($k == $data[0]-1) { echo"</tr><tr>"; $k = -1; } $k++; } } echo"</table>"; I can see how i would delete a row of information, eg delete B 1-6, but any ideas how I could delete a column? or is there a better way of storing this information? I have a table like this: http://empirebuildingsestate.com/company.html How can I separate the table values into variables so I can insert them into my mySQL database? I have been trying DOMDocument but i need some help. Hi, I have an array key whose value is a line of html in quotes, the problem is , I don't know how to do it. Lol. Any help greatly appreciated because the below code is generating an error in my editor. Thank you. Derek here is the line. Code: [Select] 'level_1'=>"<img src='<img src='sundragon_environments/ocean/ocean1_FRAME.jpg'/>'", EDIT: actually I managed to fix the error adding a semicolon after the parenthesis. Is this array correct please? Code: [Select] $currentLocation = array ( 'teardrop_ocean_bg' => array ( 'level_1'=>"<img src='<img src='sundragon_environments/ocean/ocean1_FRAME.jpg'/>'" ) ); it's not finished yet. Well, the title pretty much says it all. I'm trying to extract data from an array, put it into a 2D array and then sort it. Then I need to put it's "place" in the order into the table. I've sort of got something so far but it seems like my while loop isn't working correctly. $q="SELECT COUNT(*), horseID, total FROM enteredHorses"; $r = mysqli_query ($dbc, $q) or trigger_error(mysqli_error($dbc)); while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $count = $row['COUNT(*)']; $horse_id = $row['horseID']; $total = $row['total']; for ($counter = 1; $counter <= $count; $counter += 1) { $i = 0; $id[$i] = $horse_id; $total[$i] = $total; $i = $i+1; } } //end while array_multisort($total, SORT_DESC, $id); while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { for ($counter = 1; $counter <= $count; $counter += 1) { $i = 0; $place = '1'; $q = "INSERT INTO enteredHorses (place) VALUES ('$place') WHERE horse_id='$id[$i']'"; $r = mysqli_query ($dbc, $q) or trigger_error(mysqli_error($dbc)); $i = $i + 1; $place = $place + 1; } } I'm getting this error: "array_multisort() [function.array-multisort]: Argument #1 is expected to be an array or a sort flag " I'm really baffled. Any help? I have a table that allows users to add rows, depending on how much data they need to insert. upon submit I'd like to populate another table for review. Now I can generate the data, but I'm having trouble putting the array into a table. Here is the output from the initial page: Code: [Select] Array ( [quantity] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [description] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [unit_cost] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [shipping] => 4 [tax] => 5 [review_po] => Review ) I need it to look like this: Quantity | Description | Unit Cost 1 | 1 | 1 2 | 2 | 2 3 | 3 | 3 Tax | 4 Ship | 5 I think it's a foreach statement, but I'm not having any luck.. If someone could point me in the right direction??? i'm back again and i am not very good with tables as the <tr> and <td> tags confuse me i need to put 2 sets of links into 2 columns in the table and they come out jumbled up. Code: [Select] <table> <tr> <th>column 1</th> <th>column 2</th> </tr> <?php $sites = array_map('trim',file('websites.txt')); //read the whole file into an array & trim the newline character from the end of each line foreach ($sites as $link) { echo "<tr><td><a href='$link'>$link</a></td>"; } $sites = array_map('trim',file('websites2.txt')); //read the whole file into an array & trim the newline character from the end of each line foreach ($sites as $link) { echo "<td><a href='$link'>$link</a></td></tr>"; } ?> </table> Hi Everyone, I'm trying to figure out a way to put my MySql data into a table. I'm close to getting this completed by I'm having some trouble. The data I'm inserting isn't being put into the correct tables. I'm not quite sure what I'm doing wrong. Any help? http://whatsmyowncarworth.com/practice/table.php Code: [Select] <?php include('init.php'); $result = mysql_query("SELECT * FROM info") or die(mysql_error()); echo "<table border='1'>"; echo "<tr> <th>Name</th> <th>Last Name</th> <th>Address</th> <th>City</th> </tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td>"; echo $row['first_name']; echo "</td><td>"; echo $row['last_name']; echo "</td></tr>"; echo "<tr><td>"; echo $row['address']; echo "</td><td>"; echo $row['city']; echo "</td></tr>"; } echo "</table>"; ?> I have a dropdown menu <td width="148" height="35" valign="top"><b>Type: </b> </td> <td><select name ="tabledrop"> <option value="goggles">goggles</option> <option value="headbands">headbands</option> <input type='hidden' id='tabledrop' name='tabledrop' value = ''/> </select> </td> those 2 options are actually names of my tables. I tried this and got some syntax error. $type = $_POST['tabledrop']; $item = $_POST['goggles_name']; $price = $_POST['goggles_price']; $file = $_FILES['goggles_image']['name']; $query = "INSERT into $type SET [$type]_name = '$item' , [$type]_price = '$price' , [$type]_image = '$file'"; Hi all! Ok I am trying to put a delimited list like so , EX. item qty, item name, item price | item qty, item name, item price | item qty, item name, item price | etc. into an array so I can access it like this - $product[0] = qty, $product[1] = name, etc. My code just isnt working. This is what I have so far. $prod = array(); //breaking products text down for display $products1 = explode("|", $products); $num_prod1 = count($products1); $count = 0; foreach($products1 as $p) { $prod[] = $p; $products2 = explode(",", $p); foreach($products2 as $p2) { $prod[$count] = $prod[$count][$p2]; } $count++; } Hello all, I've run in to a very strange problem that I've never seen before. in my "config.php" file I have something like this... <?php // the URL of the script. NO trailing slash. $xconfig['url'] = $r['cfg_site_url']; // the site name $xconfig['title'] = $r['cfg_site_title']; // the YouTube user name to populate the site with. $xconfig['youtube_user'] = $r['cfg_site_youtube']; // the video to show on the homepage $xconfig['homepage_video'] = $r['cfg_homepage_video']; ?> and in my index.php page, I have <?php include "config.php"; ?> but, also in index.php, I have this <?php echo $xconfig['title'] ; ?> but it won't echo! None of the values in $xconfig[] output anything. I've even done put this in index.php <?php var_dump($xconfig); exit; ?> and that shows the array with the contents I expected. So, why does the array have the correct contents, but will not echo? Thanks a ton! |