PHP - Concatenate Field Names Into One And Return
I have the following mysql query:
Code: [Select] $lead_query=$this->db->query(" SELECT `first_name`, `last_name` , `state` FROM leads WHERE `lead_id`='$lead_id' "); $this->view->lead_query=$lead_query->fetchALL(); Now when I retrieve the above details I need to return the first_name last_name together(separated as space) and the name of the key as client_name in the array. I need it that way because when i return a json_encode($lead_query), I want to return the first_name.last_name as client_name. Similar TutorialsHi all. I've got a little function to generate a simple form depending on the input. What I'd like to do is alias the field names that get output to make them a little more human readable. However, I'm not entirely sure how to do that with the output that I get. The function.... function build_form($table_name){ $result=mysql_query("SELECT * FROM $table_name"); $num=mysql_num_rows($result); $i=1; echo "<table>"; while ($i < mysql_num_fields($result)) { $fields=mysql_fetch_field($result,$i); echo "<tr><td>" . $fields->name . "</td><td><input type=\"text\" size=\"30\" name=\"" . $fields->name . "\" /></td></tr>"; $i++; }; echo "</table>"; }; Q: Where would I have to modify the output from mysql_fetch_field? My modified function... function build_form($table_name){ $result=mysql_query("SELECT * FROM $table_name"); $num=mysql_num_rows($result); $i=1; echo "<table>"; while ($i < mysql_num_fields($result)) { $fields=mysql_fetch_field($result,$i); $field_name=str_replace("_"," ",$fields->name); echo "<tr><td>" . $field_name . "</td><td><input type=\"text\" size=\"30\" name=\"" . $field_name . "\" /></td></tr>"; $i++; }; echo "</table>"; }; Not sure if that will work, but am I heading in the right direction? Any help is much appreciated! Hi. The idea: I want to pull a client record and simply place all the row columns into editable form field text boxes. Once the user reviews and edits a submit button will update the record. What I have: Code: [Select] $Query01 = "SELECT * FROM `CustomerSignups` WHERE `Id` = '$_GET[Id]'"; $Result01 = mysql_query($Query01) or die("Error 01: " . mysql_error()); while ($get_info = mysql_fetch_row($Result01)) { foreach ($get_info as $field) echo "<p>$field</p>"; It displays as expected, a line for each field. How can I get this to pull the field names also and then I can use those as text box input fields...? I would like to echo field names in my table on webpage one by one. I know mySQL has "describe" function which will list the complete table, I am looking for a way to display each field name one by one with other stuff in between them like input field or description. I need to update a particular table and there be a whole lot of field names. I'd like to do it without having to use them the same way you can access then like $row[12] after a "select" query. The standard for updating (at least how I write it) goes like this ... Code: [Select] $query = "UPDATE linguistics SET welcome1='$var3', welcome2='$var4' WHERE language = '$lengua'"; Well, I'd like to do it where I don't have to name welcome1 and welcome2, but instead refer to their numerical position within the database. On a "select" query we can do this. Can this be done on an update? Something perhaps like ... Code: [Select] $query = "UPDATE linguistics SET 3='$var3', 4='$var4' WHERE language = '$lengua'"; ... where 3 and 4 represent the 4th and 5th fields respectively. I know that this particular phrasing doesn't work because I've tried it, but surely there has to be something. This will save me LOTS of time. Thanks!! Okay, so I'm trying to run a query to pull the information from a specific item that they click on in order to edit it. When I run an echo $query, though, it shows the field names rather than the information from the table. How can I make it so that it pulls the information rather than the field names? Here's what I have... <?php include("lib.php"); define("PAGENAME", "Edit Equipment"); if ($player->access < 100) $msg1 = "<font color=\"red\">"; //name error? $error = 0; $query = $db->execute("SELECT * FROM items WHERE id=?", array($_POST['id'])); echo "$id"; $result = mysql_query($query); echo "$query"; $data = mysql_fetch_assoc($result); $msg1 .= "</font>"; //name error? ?> Thanks!! I currently have php code that outputs information from my database, but how do i make it also display the name of the fields beside each entry of the database? heres my PHP code: Code: [Select] <?php $server = ""; // Enter your MYSQL server name/address between quotes $username = ""; // Your MYSQL username between quotes $password = ""; // Your MYSQL password between quotes $database = ""; // Your MYSQL database between quotes $con = mysql_connect($server, $username, $password); // Connect to the database if(!$con) { die('Could not connect: ' . mysql_error()); } // If connection failed, stop and display error mysql_select_db($database, $con); // Select database to use // Query database $result = mysql_query("SELECT * FROM Properties"); if (!$result) { echo "Error running query:<br>"; trigger_error(mysql_error()); } elseif(!mysql_num_rows($result)) { // no records found by query. echo "No records found"; } else { $i = 0; echo '<div style="font-family:helvetica; font-size:15px; padding-left:15px; padding-top:20px;">'; while($row = mysql_fetch_array($result)) { // Loop through results $i++; echo '<img class="image1" src="'. $row['images'] .'" />'; //image echo "Displaying record $i<br>\n"; echo "<b>" . $row['id'] . "</b><br>\n"; // Where 'id' is the column/field title in the database echo $row['Location'] . "<br>\n"; // Where 'location' is the column/field title in the database echo $row['Property_type'] . "<br>\n"; // as above echo $row['Number_of_bedrooms'] . "<br>\n"; // .. echo $row['Purchase_type'] . "<br>\n"; // .. echo $row['Price_range'] . "<br>\n"; // .. } echo '</div>'; } mysql_close($con); // Close the connection to the database after results, not before. ?> thanks in advance Hi all. The title pretty much says it all. I really have no idea if this is even possible or where to start. A recent dilemma caused me to have to change a field name with some spaces in it. I have a database with ~65 tables, a majority of which were converted from Access. So I'm curious as to how many others have bad field names. Is there a way to use PHP to go through all the field names in a database and remove spaces? This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=318312.0 Hi all, I have the following MySQL insert query: Code: [Select] $insert= mysql_query ("INSERT INTO tablename (column1,`".$EXPfields."`) VALUES ('$something','".$EXPvalues."')"); where $EXPfields is an array of table-field-names and $EXPvalues is an array of table-field-values. Now I want to write an equivalent query, but using UPDATE instead of INSERT INTO, but I don't want to write out all the field names/values separately, but again want to use $EXPfields and $EXPvalues. So something like this: Code: [Select] $update = mysql_query ("UPDATE tablename SET (column1,`".$EXPfields."`) = ('$something','".$EXPvalues."') WHERE .... "); Is this possible? If so, what is the proper syntax? Thanks! ok, this is clearly 1st grade code for some, but i'm not there yet - I'm querying posts in WordPress, and the post_content will always have an image in the beginning of the post followed by the content. i don't want to get the image, just the content that's after the image, which is wrapped in anchor tags, of course. Code: [Select] <a href="http://path/to/image.jpg"><img src="http://path/to/image.jpg" /></a> <p>Post content yadda, yadda, hoowie</p> obviously a character count won't work, so i need to get anything that follows the first "</a>", say...? is this the best way, or is there an easier way? thanks for anyone's help. GN 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 phpsaneWhy won't '../stuff.com' concatenate? Code: [Select] <?php echo '<img src="', '../stuff.com'.getUserAvatar($post['username']), "\" class=\"avatar small\" title=\"${displayName}\" alt=\"${displayName}\" />"; ?> I know that usually when we concatenante in PHP we use the period . to concatenate but I have been watching a few videos and they seems to use a comma , instead of .
I didn't know about the comma before and if I didn't hear it wrong the video says both would work just depend on the way you work.
So I tried using comma for testing such as
echo '<pre>' , print_r($var) , '</pre>';which works fine but when I use it in a function for fun such as function dd($var){ return '<pre>' , print_r($var) , '</pre>'; }this gives me errors about the comma but if I use period as concatenate the page would not return as html tag <pre> I know that I can just use var_dump and I tried searching things like difference between , and . in php or something similiar I couldn't find a page to explain the major difference. Can someone give me a hand? Sorry if this question is too stupid though. First project in php and mysql, I've searched but can't find the answer to stringing together two numbers and a "-" without php giving me the results as it thinks I'm creating an equation. I need the string as text. Example, I need the following as a text string, id_have (INT) and id_have (INT) joined together as id_have-id_want, or, as they will be numbers, 23-45. When I use this; $havewant = $id_have . "-" . $id_have; The result will be $id_have (23) minus $id_want (45), I get -22, not what I'm looking for!!! I need the text string of "23-45" to store in a table. Thanks! I need to put this inside of a p tag so I can change some properties but everything I've tried doesn't work and just shows nothing.
I've tried this:
printf('<p style="text-align: left; width: 500px;">', htmlspecialchars($fetch['shout'], ENT_QUOTES, 'UTF-8'), '</p>');This is what I need to be wrapped in p tags: htmlspecialchars($fetch['shout'], ENT_QUOTES, 'UTF-8');EDIT: I've noticed the code below works but when I style it inside of the tag the text won't show, should I include a CSS file on the PHP file? echo "<p>".htmlspecialchars($fetch['shout'])."</p>\n";EDIT: I just needed to create the CSS for the p tags in the index and it worked perfectly fine. Sorry for the pointless thread. Edited by Alanay, 18 December 2014 - 09:14 AM. I have the variables $cap and $url If the $cap contains any of the strings ".jpg,.JPG,.gif,GIF" I have to concatenate $url.$cap Hello I have an array where i want the join values (not sum) where the date are the same This my array(var_export) array ( 0 => array ( 0 => array ( 0 => array ( 'dataAula' => '2020-09-21', 'tempos' => '4', 'tempos2' => 'Segunda', 'total' => 4, ), ), 1 => array ( 1 => array ( 'dataAula' => '2020-09-22', 'tempos' => '4', 'tempos2' => 'Terça', 'total' => 8, ), ), 2 => array ( 2 => array ( 'dataAula' => '2020-09-28', 'tempos' => '4', 'tempos2' => 'Segunda', 'total' => 12, ), ), 3 => array ( 3 => array ( 'dataAula' => '2020-09-29', 'tempos' => '4', 'tempos2' => 'Terça', 'total' => 16, ), ), 4 => array ( 4 => array ( 'dataAula' => '2020-10-06', 'tempos' => '4', 'tempos2' => 'Terça', 'total' => 20, ), ), 5 => array ( 5 => array ( 'dataAula' => '2020-10-12', 'tempos' => '4', 'tempos2' => 'Segunda', 'total' => 24, ), ), 6 => array ( 6 => array ( 'dataAula' => '2020-10-13', 'tempos' => '4', 'tempos2' => 'Terça', 'total' => 28, ), ), 7 => array ( 7 => array ( 'dataAula' => '2020-10-19', 'tempos' => '4', 'tempos2' => 'Segunda', 'total' => 32, ), ), 8 => array ( 8 => array ( 'dataAula' => '2020-10-20', 'tempos' => 1, 'tempos2' => 'Terça', 'total' => 33, ), ), ), 1 => array ( 0 => array ( 0 => array ( 'dataAula' => '2020-10-20', 'tempos' => 3, 'tempos2' => 'Segunda', 'total' => 3, ), ), 1 => array ( 1 => array ( 'dataAula' => '2020-10-27', 'tempos' => '4', 'tempos2' => 'Terça', 'total' => 7, ), ), 2 => array ( 2 => array ( 'dataAula' => '2020-11-02', 'tempos' => '4', 'tempos2' => 'Segunda', 'total' => 11, ), ), 3 => array ( 3 => array ( 'dataAula' => '2020-11-03', 'tempos' => '4', 'tempos2' => 'Terça', 'total' => 15, ), ), 4 => array ( 4 => array ( 'dataAula' => '2020-11-09', 'tempos' => '4', 'tempos2' => 'Segunda', 'total' => 19, ), ), 5 => array ( 5 => array ( 'dataAula' => '2020-11-10', 'tempos' => '4', 'tempos2' => 'Terça', 'total' => 23, ), ), 6 => array ( 6 => array ( 'dataAula' => '2020-11-16', 'tempos' => '4', 'tempos2' => 'Segunda', 'total' => 27, ), ), 7 => array ( 7 => array ( 'dataAula' => '2020-11-17', 'tempos' => '4', 'tempos2' => 'Terça', 'total' => 31, ), ), 8 => array ( 8 => array ( 'dataAula' => '2020-11-23', 'tempos' => '4', 'tempos2' => 'Segunda', 'total' => 35, ), ), 9 => array ( 9 => array ( 'dataAula' => '2020-11-24', 'tempos' => '4', 'tempos2' => 'Terça', 'total' => 39, ), ), 10 => array ( 10 => array ( 'dataAula' => '2020-11-30', 'tempos' => '4', 'tempos2' => 'Segunda', 'total' => 43, ), ), 11 => array ( 11 => array ( 'dataAula' => '2020-12-07', 'tempos' => '4', 'tempos2' => 'Segunda', 'total' => 47, ), ), 12 => array ( 12 => array ( 'dataAula' => '2020-12-14', 'tempos' => '4', 'tempos2' => 'Segunda', 'total' => 51, ), ), 13 => array ( 13 => array ( 'dataAula' => '2020-12-15', 'tempos' => '4', 'tempos2' => 'Terça', 'total' => 55, ), ), 14 => array ( 14 => array ( 'dataAula' => '2021-01-04', 'tempos' => '4', 'tempos2' => 'Segunda', 'total' => 59, ), ), 15 => array ( 15 => array ( 'dataAula' => '2021-01-05', 'tempos' => '4', 'tempos2' => 'Terça', 'total' => 63, ), ), 16 => array ( 16 => array ( 'dataAula' => '2021-01-11', 'tempos' => 4, 'tempos2' => 'Segunda', 'total' => 67, ), ), ), 2 => array ( 0 => array ( 0 => array ( 'dataAula' => '2021-01-12', 'tempos' => '4', 'tempos2' => 'Terça', 'total' => 4, ), ), 1 => array ( 1 => array ( 'dataAula' => '2021-01-18', 'tempos' => '4', 'tempos2' => 'Segunda', 'total' => 8, ), ), 2 => array ( 2 => array ( 'dataAula' => '2021-01-19', 'tempos' => '4', 'tempos2' => 'Terça', 'total' => 12, ), ), 3 => array ( 3 => array ( 'dataAula' => '2021-01-25', 'tempos' => '4', 'tempos2' => 'Segunda', 'total' => 16, ), ), 4 => array ( 4 => array ( 'dataAula' => '2021-01-26', 'tempos' => '4', 'tempos2' => 'Terça', 'total' => 20, ), ), 5 => array ( 5 => array ( 'dataAula' => '2021-02-01', 'tempos' => '4', 'tempos2' => 'Segunda', 'total' => 24, ), ), 6 => array ( 6 => array ( 'dataAula' => '2021-02-02', 'tempos' => '4', 'tempos2' => 'Terça', 'total' => 28, ), ), 7 => array ( 7 => array ( 'dataAula' => '2021-02-08', 'tempos' => '4', 'tempos2' => 'Segunda', 'total' => 32, ), ), 8 => array ( 8 => array ( 'dataAula' => '2021-02-09', 'tempos' => 1, 'tempos2' => 'Terça', 'total' => 33, ), ), ), )[] So, for example i have repeated the date 2020-10-20, one with "tempos =1" and another with "tempos=3" I'm looking to have only one date with concatened tempos field Example array ( 'dataAula' => '2020-10-20', 'tempos' => 1+3, 'tempos2' => 'Terça', 'total' => 33, ), So what i have now is this code but not getting what i need
foreach ($finalCronograma as $sub) { $row = current($sub); if (isset($result[$row[0]['dataAula']])) { $result[$row[0]['dataAula']]['tempos'] .= '+' . $row[0]['tempos']; } else { $result[$row[0]['dataAula']] = $sub; } }
any help? This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=313826.0 I'm trying to get a php script working to download the latest CNN news podcast each hour. CNN names the file based on the year, month, day, and time. Here's what I'm trying: Code: [Select] <?php $year = date('Y'); $month = date('m'); $day = date('d'); $now = date('Y-m-d-h'); $hour = date('gA'); $hourplus1 = ($hour + 1); $hourminus1 = ($hour - 1); $ampm = date('A'); $url = "http://podcasts.cnn.net/cnn/services/podcasting/newscast/" . "audio/" . "$year" . "/" . "$month" . "/" . "$day" . "/CNN-News-" . "$month" . "-" . "$day" . "-" . "$year" . "-" . "$hourplus1" . "$ampm" . ".mp3"; echo $url; echo system('wget "$url"'); ?> When running from the shell, I get an "http://: invalid hostname" error. The echo of $url looks right...but it won't run from shell. Any ideas? |