PHP - Get Only First Coulmn To Concat With Each Second Column Rows From Csv File Using Php
Hi all,
Similar TutorialsI have an existing two table structure where sub-data is related to main data in a many-to-one relationship.
There are several identically named columns in both tables.
(The PHP database class will return an associative array that did not deal with identical keys in the row.)
I'm LEFT JOINing the sub-data to the main-data tables.
When using a stand-alone query browser (to experiment), the display shows all columns, even those column names that appear more than once in the row.
Is there a general approach to making keys (column names) unique strictly using a MySQL SELECT statement?
Perhaps using table aliases, how can I concat the alias to the columns that come from each table?
What I am trying to avoid is giving an alias to each column in a list of column names, such as:
SELECT
C.name AS C_name
C.numb AS C_numb
D.name AS D_name
D.numb AS D_numb
etc.
I am wanting more like the result one could guess would be from:
C.* AS C_*
D.* AS D_*
Or better:
* AS CONCAT(__TABLE__, '_', *)
if __TABLE was a magic constant (like PHP's __LINE__, __FILE__, etc).
Hi Everyone, I am working on implementing a blog comment system using the query below to display comments. My question is what is the best way to do a row count for the id column with this query or do I need to do a second db query to accomplish this? The purpose of this is to echo out the number of comments for that post, before displaying them. Any help is appreciated. Thanks in advance, kaiman <?php // get url variables $post_id = mysql_real_escape_string($_GET['id']); include ("../../scripts/includes/nl2p.inc.php"); // connects to server and selects database include ("../../scripts/includes/dbconnect.inc.php"); // table name $tbl_name3="blog_comments"; // select info from comments database $result3 = mysql_query ("SELECT count(*) FROM $table_name3 WHERE id='$post_id' ORDER BY id DESC LIMIT 1") or trigger_error("A mysql error has occurred!"); if (mysql_num_rows($result3) > 0 ) { while($row = mysql_fetch_array($result3)) { extract($row); // display number of comments // display date $row_date = strtotime($row['date']); putenv("TZ=America/Denver"); echo "<p class=\post\">On ".date('F, jS, Y', $row_date)." "; // display commenter name if($row['url'] == "") { echo $row['name']." wrote:</p>\n"; } else { echo "<a href=\"".$row['url']."\" target=\"_blank\">".$row['name']."</a> wrote:</p>\n"; } // display content $comments = $row['comment']; echo nl2p($comments); } } else { echo "<p class=\"large_spacer\">No Comments</p>"; } ?> I have a mysql table that looks like this: id int(11) autoincrement artist varchar(50) title varchar(50) I need to check for any rows that have the same titles. So for example, if I had the following rows: 1 - Bob - Hello World 2 - Charlie Brown - Theme Song 3 - Joe - Hello World 4 - Joe - Is Cool 5 - Bob - Magic Dude The query would display Row 1, and Row 3 as duplicates. Any idea how I can do something like this? I've been using this code for a long time and realised it's very repetitive, but the id_column I want changes all the time function getRows() { $query = mysql_query($sql); $rows = array(); while($row = mysql_fetch_assoc($query)) { $rows[$row['id_column']] = $row; } return $rows; } So I wrote this function that will automatically create an array with a column I choose if I wish, but I'm not sure if it's very efficient. function getRows($query, $column_id = false) { $rows = array(); while($row = mysql_fetch_assoc($result)) { if($column_id === false) { $rows[] = $row; } else { if(isset($row[$column_id])) { $rows[$row[$column_id]] = $row; } else { $rows[] = $row; } } } return $rows; } I would appreciate some input as to make it better. Thanks. How to split the attribute-options after row 15 so it show attribute 1-15 than split the listing than show row 16-30 in a new column split again and show the next 15 attributes and list them in a new column? Also how to remove the space (3 mm) between each row Code: [Select] echo '<tr><td><ul><li style="list-style: none; "><input type="text" name='.$i.'_quantity value="" style="text-align:right;" size="3"> '; echo tep_draw_hidden_field($i.'_id[' . $products_options_name['products_options_id'] . ']', $products_options_array[$i]['id']) . $products_options_array[$i]['text']; echo "</li></ul></td></tr>";[code] Please help me. I'm fairly new to php and have been looking for answers on Goog for about two weeks or so with not an answer. This is my first post on a forum of any type. I have a form on a php page that when submitted sends a value to a confirm php page Code: [Select] <form action='buyconfirmorder.php' method='post'> Purchase: <input type='text' name='ask_shares'><br /><br /> You Will Pay:  <input type='text' name='ask'><br /><br /> <input type='hidden' name='postcardname' value='$postcardname'> <input type='submit' name='submit' value='Order'> </form> I need to take the users input from the form and subtract each row from a mysql DB one at a time until the users input is down to a remainder and then I need the remainder (I'll be working with the remainder as well). Code: [Select] $query = mysql_query(" SELECT * FROM ask, search WHERE search.id = ask.card_search_id AND search.card_name = '$postcardname' ORDER BY ask_price ASC"); The array will be numbers only. So an example would be: User input from the form is 100.00 Code: [Select] while($rows = mysql_fetch_array($query)) { $ask_price = $rows['ask_price']; } Let's say the array is 50.50, 40.50, 35.00 I need some code to do: 100.00 - 50.50 = 49.50 49.50 - 40.50 = 9.00 9.00 - 35.00 = <0 does not do anything so remainder = 9.00 would it be something like Code: [Select] $new_ask_shares = $_POST['ask_shares']; //some sort of a loop or array or combo of both ($new_ask_shares - $ask_price); the numbers are just an example as the actual numbers will be different each time I query my DB. Thanks in advance im trying to pull the information for a post which is to include 3 db tables(posts, users, attachments) Ive tried the following code: Code: [Select] $query = $link->query("SELECT p.*, u.u_username, u.u_posts, u.u_avatar, u.u_signature, u.u_avatar_cropped, group_concat(a.a_name, a.a_size, a.a_date_posted separator '<br />') attachments FROM ".TBL_PREFIX."posts as p JOIN ".TBL_PREFIX."users as u ON (u.u_username = p.p_poster) LEFT JOIN ".TBL_PREFIX."post_attachments as a USING (".TBL_PREFIX."posts.p_pid) WHERE p.p_tid = '$tid' GROUP BY p.p_pid ORDER BY p.p_time_posted ASC")or die(print_link_error()); but that generates the error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.p_pid) WHERE p.p_tid = '158' GROUP BY p.p_pid ORDER BY p.p_time_pos' at line 10" All i am trying to do is pull everything from posts, join users and select all attachments that belong to the post. Here is my database setup(shortened) asf_posts p_pid | p_name | p_poster 1 test doddsey65 asf_users u_uid | u_username 1 doddsey65 asf_attachments a_aid | a_pid | a_name 1 1 name can anyone help? This topic has been moved to Other Libraries and Frameworks. http://www.phpfreaks.com/forums/index.php?topic=355753.0 I am trying to print out rows of images and pieces them to form a bigger map but having a little trouble printing out correctly. I am doing the following... A numbered text file with rows of numbers like these: 33.txt Code: [Select] 48,48,49,49,48,47,48,49,48,50,50,48,50,49,48,49,49,49 49,11,4,8,50,11,4,4,8,48,11,4,4,8,48,11,8,50 ... The number represent different area and the number in the text file represent each individual pieces of that particular area. the number of rows and columns are arbitrary. Example: Here is my code at the moment but there is something wrong in it as I can't seems to correctly print everything out... Code: [Select] <?php $file = file_get_contents("33.txt"); // can be any numbered text files $entries = explode("\n", $file); echo "Entries <br /><hr />"; for($i=0;$i < sizeof($entries);$i++){ for($j=0;$j < sizeof($entries);$j++){ $data = explode(",", $entries[$j]); echo "<td align=\"center\" background=\"test/33_"; echo $data[$j]; echo ".gif\" width=\"65\" height=\"65\"> <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"65\" height=\"65\"> <tbody><tr> <td> <center></center> </td> </tr></tbody> </table> </td>"; } } ?> What I'm trying to do is allow people to change the order of their navigation bar. I don't have a problem doing this using a listbox to move the values up and down. But to start me off, I'd like to know how I can use CONCAT or explode to draw data from the DB and list it. To better understand: I have a database with a table called "navbar". I have the fields: ID, name, link, icon, caption What we'll be looking at most is the ID. The ID's range from 1-19, so 19 links on the navbar. They are extracted from the database like so: $navbar=$db->execute("select * from navbar"); for($i=1;$nav=$navbar->fetchrow();$i++) { $name=$nav['name']; $link=$nav['link']; $title=$nav['caption']; $image=$nav['icon']; ?> <tr> <td background="images/sub_btn.gif" class="norepeat" valign=top> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td></td> <td><img src="images/spacer.gif" width="198" height="1"></td> </tr> <tr> <td><img src="images/spacer.gif" width="1" height="19"></td> <td style="padding-left: 10px;" class=sub><img src=<?=$image?> width=13 height=13 align=absmiddle> <a target=middle href="<?=$link?>" class="sub_lnk" title='<?=$title?>'><?=$name?></a> </td> </tr> </table></td> </tr> <?}?> This is for my online game, so we then go to my players table and add a field called "navorder". The default value is: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 How would I, just for tests, lists the values from the navbar table, by ID, on the page? Example: My value is set to 1,2,3,4,5 When drawing the data, however I do it, would show: Link1 Link2 Link3 Link4 Link5 Now if I changed the order, with a listbox which I can do, would change it to 3,1,5,2,4 It would then show: Link3 Link1 Link5 Link2 Link4 Sorry if it seems confusing, I tried my best at explaining it. Someone told me to use the explode function, or CONCAT. I tried to read up on explode() but just got confused as to how to use it. If someone could tell me how to do this it'd be much appreciated Thanks! I am trying to write post variables to a csv file but it writes everything in one line separated by comma <?php $list= array($_POST['purchases']); $file = fopen("purchases.csv", "w"); foreach ($list as $line) { fputcsv($file, $line); } fclose($file); ?> Result in purchases.csv file Marilyn,Nancy,Johan,Carol,Juanic,Shirley But I want every string value on separated line Marilyn Nancy Johan Carol Juanic Shirley
This is a continuation of my previous post. I would like to be able to read the second column of a tab delimited txt file into a variable as a string. let's say i have: Code: [Select] somegarbage_here readthisinto$file I'm looking for this but all tuts seem to be about importing it to arrays and what not. I just want to import the second column. There is always one line. what Im basically trying to do is just like a phpmyadmin function... you select rows you want to update with a checkbox and then it takes you to a page where the rows that are clicked are shown in forms so that you can view and edit info in them... and then have 1 submit button to update them all at once. I have 2 queries that I want to join together to make one row
My Php Buddies, I have mysql tbl columns these:
id: Now, I want to display their row data by excluded a few columns. Want to exclude these columns: date_&_time account_activation_code account_activation_status id_verification_video_file_url password
So, the User's (eg. your's) homepage inside his account should display labels like these where labels match the column names but the underscores are removed and each words' first chars CAPITALISED:
Id: 1
For your convenience only PART 1 works. Need help on Part 2 My attempted code:
PART 1 <?php // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Query to get columns from table $query = $conn->query("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'members' AND TABLE_NAME = 'users'"); while($row = $query->fetch_assoc()){ $result[] = $row; } // Array of all column names $columnArr = array_column($result, 'COLUMN_NAME'); foreach ($columnArr as $value) { echo "<b>$value</b>: ";?><br><?php } ?> PART 2 <?php //Display User Account Details echo "<h3>User: <a href=\"user.php?user=$user\">$user</a> Details</h3>";?><br> <?php $excluded_columns = array("date_&_time","account_activation_code","account_activation_status","id_verification_video_file_url","password"); foreach ($excluded_columns as $value2) { echo "Excluded Column: <b>$value2</b><br>"; } foreach ($columnArr as $value) { if($value != "$value2") { $label = str_replace("_"," ","$value"); $label = ucwords("$label"); //echo "<b>$label</b>: "; echo "$_SESSION[$value]";?><br><?php echo "<b>$label</b>: "; echo "${$value}";?><br><?php } } ?> PROBLEM: Columns from the excluded list still get displayed. Edited November 19, 2018 by phpsane$sql = "SELECT * from updates ORDER BY id DESC LIMIT 3"; $stmt = $link->prepare($sql); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows = '0') { echo "There haven't been any updates yet."; } else { echo "There are news posts"; }
Gives me the error "Cannot write property in /home/evoarena/public_html/Dev/news.php:14 ", line 14 is the if statement. How can I fix it? how to get the name of the file including a file from the included file, This one has me mixed up a bit.. I am trying to record site activity information from a common.php file using a user object. But since the file is included into different php files based on different situations I need a dynamic way of finding the file name that is including it. I could be over complicating things but right now this seems like the best solution other wise I'll have to rewrite the code on every page i write. Is there a function for doing this? Or if someone gets what I'm trying to do if they could point me to the direction of some more information on it. Thanks. How can I get the next four and previous four rows? Here is what I have and it isn't working at at all. $q1 = Mysql::query("SELECT id, file, dir FROM photos WHERE id > $id AND album = '$al' ORDER BY id ASC LIMIT 4"); $q2 = Mysql::query("SELECT id, file, dir FROM photos WHERE id < $id AND album = '$al' ORDER BY id DESC LIMIT 4"); can someone please give me some guidance on how to do this please I am wanting to create a status updating type application on my site and i have this idea in my head i want it to retrive and print the last 3 posts (max ids) made by the user if someone could give me some example code please and i can hopefully work from that. Thanks James |