PHP - Concatenation With Strings And Arrays
I can successfully concatenate strings and vars to assemble a php var using an echo statement like this:
Code: [Select] echo "<input type='radio' id='radio_btn_" . $Row['question_number'] . "_0' name='question[question_" . $Row['question_number'] . "]' value='0' checked='checked' />"; However, when attempting to reference the same var in an IF statement, I can't seem to get the syntax right. I know this syntax is wrong, but I list it for reference to describe the issue I am trying to solve. Code: [Select] if ($_POST['radio_btn_' . $Row['question_number'] . '_0'] == '0'){ I also tried curly braces but can't arrive at a solution. Any advise would be appreciated. Similar Tutorials
Is there a subtle difference between Thank you. Hello, I'm working on a script for my chat software that will convert an array into a string and then can easily later on be put back into an array, I've already done this but when I did it I didn't think of sub-arrays. For example of an array: Code: [Select] $user_info = array( 'username' => 'test', 'other_info' => array( 'var' => 'value', 'var2' => 'value2' ) ); and that would change into something like this: user_info:{username:test, other_info:{var:value,var2:value2}} I do not care what the string looks like, above was just an example, any separators can be used. I just can't have it messed up by user input (a username with a character of a separator or something). The code I have works for this: Code: [Select] $user_info = array('id' => 1, 'username' => 2); and it outputs this as the string id=1&username=2& The code I use is: Code: [Select] function strtoarray($str) { $info = explode("&", $str); if (is_array($info)) foreach ($info as $data) { $data = explode("=", $data); if (is_array($data)) { if (!empty($data[0])) { if ($array[$data[0]]) { if (!is_array($array[$data[0]])) { $array[$data[0]] = array($array[$data[0]]); } $array[$data[0]][] = stripslashes(urldecode($data[1])); } else { $array[$data[0]] = stripslashes(urldecode($data[1])); } } } } return $array; } function arraytostr($array) { foreach ($array as $key => $value) { $str .= $key . "=" . addslashes(urlencode($value)) . "&"; } return $str; } I'm really not sure if regex is the way to go with this or what and that's really why I'm asking for help. I know I could do this with more explodes and what not but I feel as if there's probably a better way to do this and I'm just over complicating it. Thank you in advance.
Hi, simply put;
$abs="Bob Rob Sue";
how can I put into code, when "Bob1:2" is used, it finds a match of "Bob", and assigns the complete string of "Bob1:2" ? <?php $abs="Bob Rob Sue"; $abby="sometext Bob1:2 is Sue3:4 where Rob2:1 etc"; $nums = preg_replace('/[a-zA-Z]/', ' ', $abby); $lets = preg_replace('/[0-9]:?[^0-9]/', ' ', $abby); //$lets1=array_intersect($abs, $lets); $absArr = explode(' ', $abs); $abbyArr = explode(' ', $lets); $letsx=array_intersect($absArr, $abbyArr); //echo $letsx[0] . "</br>"; //############################################# $nums1='/(\d+\:\d+)/s'; preg_match_all($nums1, $nums, $nums2); foreach($nums2 as $numsx){ } $abi0="$letsx[0]+$numsx[0]"; $abi1="$letsx[1]+$numsx[1]"; $abi2="$letsx[2]+$numsx[2]"; $answer=$abi0."</br>".$abi1."</br>".$abi2."</br>"; echo $answer; ?>
current results: Hi - I am passing $_POST data from an HTML form. The POST array looks like this: Code: [Select] Array ( [location] => Array ( [0] => Collingwood [1] => Varsity ) [4] => 01 Apr 2012 [1] => 12 Apr 2012 [2] => 01 May 2012 [3] => 01 Jun 2014 [8] => 01 Jul 2012 [6] => 12 Aug 2012 [7] => 20 Apr 2013 [5] => 20 Apr 2014 [submit] => Save Changes ) I am using a foreach loop to separate the keys from the values. The $values include dates and locations. I then pass $values to strtotime to convert the dates to a Unix Time stamp. when strtotime sees the Locations - it throws a PHP error "strtotime() expects parameter 1 to be string, array given" so my question is what can I do to separate the Dates from the locations in order to avoid the error ? MANY THANKS to anyone who can give advice on this ! How do I add a space in my concatenation? What I have down below doesn't work. $result[0] .$result[1] I have a table (applicants) which has a number of fields including 5 for potential 'parties' - these are party1, party2, party3, party4 and party5. Any particular applicant will always have two parties but could have any number up to 5. I need to export a pdf (which I can do) but I need it to include data drawn from the applicants table in the format: party1, party2 and party3 OR party1 and party2 etc that is, each party has a comma after it except the last but one, which has no comma but an 'and'. (I hope this is clear enough!) I can't work out how to do this for a varying number of 'parties'. Any suggestions/answers gratefully received. I am currently learning Zend/PHP on the fly. I've gone from 0 to 40 in about 2 months, as far as savviness is concerned. I am working on a form that I want to display array information of two things (shortname and fullname) in a drop-down list. I am thinking I need to do some sort of concatenation to achieve this, or would I do an array within an array? The array is defined in a value object and that draws the id, shortname and fullname. Shortname is the defined label in that file. In the form page, i have the LO ['full_name'] => LO ['label']... this inputs the right information into the database, but shows the full name. I want the drop-down to show it as following: Shortname|Fullname. This will be used in a couple of places once I get on the right track on how to accomplish this. I would be accepting of any help, as well as any resourced that could answer my question and help me finish this form. Cheers, ESK problem is when i select unpaid in drop down box it not show the expected result. I dont know wats wrong here I doubt $reg .=" AND status=0 "; find wats wrong Thanks a lot Code: [Select] <?php include("connection.php"); $reg=""; if(isset($_POST['filter'])) { if($_POST['sel'] == "0") { $reg .=" AND status=0 "; } elseif($_POST['sel'] == "1") { $reg .=" AND status=1 "; } elseif($_POST['sel'] == "3") { $reg .=" AND (status=0 OR status=1) "; // echo "<a href='vudetails.php?regno=".$qry2['reg_no']."'>".$qry2['name']."</a>"; } $filter=mysql_query("SELECT * FROM stu_details WHERE dept LIKE '%".$_POST['dept']."%' OR course LIKE '%".$_POST['cors']."%' OR YEAR LIKE '%".$_POST['year']."%' OR room_no LIKE '%".$_POST['dept']."%' OR reg_no LIKE '%".$_POST['dept']."%' OR doj BETWEEN '".$_POST['dept']."' AND '".$_POST['dept']."'".$reg); while($f2=mysql_fetch_array($filter)) { echo "<a href='vudetails.php?regno=".$f2['reg_no']."' >".$f2['name']."</a> <br />"; } } ?> <html><head><title>Filter</title></head> <script type="text/javascript" src="datetimepicker.js"> </script> <script type="text/javascript"> function validate() { var a=document.forms['form']['dept'].value; if (a==null || a=="") { alert("Please enter a search."); return false; } /*else { return true; }*/ } </script> <form name="form" action="" method="post"> <table border="0"> <tr><td> <select name="sel"> <option value="3">All </option> <option value="0">Unpaid </option> <option value="1">Paid </option> </select> <tr><td> Department: <input type="text" name="dept"> <tr><td> Course: <input type="text" name="cors"> <tr><td> Year: <input type="text" name="year"> <tr><td> Room No: <input type="text" name="roomno"> <tr><td> Register No: <input type="text" name="regno"> <tr><td> Date of Join: <input type="text" name="doj" id="from"> <a href="javascript:NewCal('from','ddmmyyyy')"> <img src="cal.gif" width="16" height="16" border="0" alt="Pick a date"></a> To: <input type="text" name="datto" id="todat"> <!--<tr><td>Date: <input text="text" id="dp" name="dp" size="25">--> <a href="javascript:NewCal('todat','ddmmyyyy')"> <img src="cal.gif" width="16" height="16" border="0" alt="Pick a date"></a> <tr><td> <!--<input type="submit" name="filter" value="Filter" onclick="return validate()">--> <input type="submit" name="filter" value="Filter" onclick="validate()"> </table> </form> </html> I am using the code posted here to do something with a third party software
http://www.interwork...trusted-tickets
however its utterly frutrating that something as simple as
$fullUrl = "http://{$server}/trusted/{$ticket}/{$url}";
returns me the following
moOaH6BAKFMGGszd_IQHTvpAhttp://<server>/trusted/1/views/<dashboard>?format=png
instead of
http://<server>/trusted/moOaH6BAKFMGGszd_IQHTvpA/views/<dashboard>?format=png
completely beats me. Any idea why ? Is it something to do with the variable $ticket which has the value of moOaH6BAKFMGGszd_IQHTvpA in this case ?
I am trying to figure out how to have an if statement in the middle of strings I am trying to concatenate. I have a from that has a address1 and address2, where address2 often does not exist. So I want to only add that line if there is actually content. I was trying something like the following, but that is giving me an error. I am pretty new to php, so I can't seem figure it out. Any help would be greatly appreciated. Code: [Select] "<p><strong>Address 1: </strong><br>" . $address1 . "<br>" if ( isset($address2) ) { echo . $address2 . "<br>" } . $city . ", " . $state . " " . $zip . "</p>" i want to do it with cURL-Multi since this is the most pretty way to do it! Well i am not sure bout the string concatenation. How to do it - Note i want to fetch several hundred pages: see the some details for this target-server sites - /(i have to create a loop over several hundred sites) siteone_dot_com?show_subsite=9009 siteone_dot_com?show_subsite=9742 siteone_dot_com?show_subsite=9871 and so on and so fort how to appy this loop into the array of the curl-multi <?php /************************************\ * Multi interface in PHP with curl * * Requires PHP 5.0, Apache 2.0 and * * Curl * ************************************* * Writen By Cyborg 19671897 * * Bugfixed by Jeremy Ellman * \***********************************/ $urls = array( "siteone", "sitetwo ", "sitethree" ); $mh = curl_multi_init(); foreach ($urls as $i => $url) { $conn[$i]=curl_init($url); curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,1);//return data as string curl_setopt($conn[$i],CURLOPT_FOLLOWLOCATION,1);//follow redirects curl_setopt($conn[$i],CURLOPT_MAXREDIRS,2);//maximum redirects curl_setopt($conn[$i],CURLOPT_CONNECTTIMEOUT,10);//timeout curl_multi_add_handle ($mh,$conn[$i]); } do { $n=curl_multi_exec($mh,$active); } while ($active); foreach ($urls as $i => $url) { $res[$i]=curl_multi_getcontent($conn[$i]); curl_multi_remove_handle($mh,$conn[$i]); curl_close($conn[$i]); } curl_multi_close($mh); print_r($res); ?> I'm wondering if this is at all possible:- Code: [Select] $i = 7; $row_item['comment$i_name'] = "john"; with the $i in $row_item['item$i_id'] being replaced with whatever $i is set as sort of like this:- $row_item['comment.'$i'._name'] Is this sort of concatenation possible inside array element names as I can't seem to get it to work? Hi - good evening! thanks for the answer and all the help so far Oncemore the "Fetching-part" for a little parser-script: Here we go - new target urls: see the overview: http://dms-schule.bildung.hessen.de/index.html http://dms-schule.bildung.hessen.de/suchen/suche_schul_db.html Search by pressing the button "type" and then choose all schools with the mouse! Results 2400 schools: Here i can provide some "more help for getting the target!" - btw: see some details for this target-server: http://dms-schule.bildung.hessen.de/suchen/suche_schul_db.html?show_school=9009 http://dms-schule.bildung.hessen.de/suchen/suche_schul_db.html?show_school=9742 http://dms-schule.bildung.hessen.de/suchen/suche_schul_db.html?show_school=9871 well - you see i have to iterate over the sites - with a function /(a loop) http://dms-schule.bildung.hessen.de/suchen/suche_schul_db.html?show_school=1000 to 10000 BTW - after fetching the page i have to see which one are empty - those ones do not need to be parsed! Well - i want to do this with curl-multi since this is the most advanced way to do this: I see i have an array that can be filled I have to try it out...!! Well, I'm not pretty sure - but judging by the low level of PHP i have: I think that i may need a double array to define the URLs like: Can i do like so?: $urls = array( "siteone" => "suche_schul_db.html?show_school=%i", "sitetwo" => "suche_schul_db.html?show_school=%i", "sitethree" =>"suche_schul_db.html?show_school=%i" ); $params = array ( for ($i = 1; $i <= 10000; $i++) { // body of loop } // well i have to define the variables in a open form like abvove nand not below: // better than doing it like so: "siteone" => array(9009, 9742, 9871), ); then pass them to curl-multi as: foreach ($urls as $id => $url) { foreach ($params[$id] as => $param) { $i = $id . $param; $finalurl = sprintf($url, $param); $conn[$i] = curl_init($finalurl); I don't know if that's fits the needs tip that i am looking for but I hope so ;-) Can i do like so [see above]? What do yu think. i send you many greetings martin Hello dear folks i am currently workin on a little parser with cURL - and i have some questions he How to do the String concatenation with PHP ; note i wanna do it with curl! the target URL: http://www.schulministerium.nrw.de/BP/SchuleSuchen?action=572.8745475728288 click all checkbuttons Results: approx 6400 results Here i can provide some "more help for getting the target!" - btw see three detail page: http://www.schulministerium.nrw.de/BP/SchuleSuchen?action=261.2855969084779&SchulAdresseMapDO=116191 http://www.schulministerium.nrw.de/BP/SchuleSuchen?action=261.2855969084779&SchulAdresseMapDO=116270 http://www.schulministerium.nrw.de/BP/SchuleSuchen?action=261.2855969084779&SchulAdresseMapDO=188268 btw: we can loop over the results - with a iteration - especially i wanna know: how to do the String concatenation with PHP ; note i wanna do it with curl! function get_page_data($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch); if($output!=false && $_POST['dt']=='No') return $output; curl_close($ch); } for($i=1;$i<=$match[1];$i++) { $url = "http://www.example.com/page?page={$i}"; $data = get_page_data($url); if($data) { $cleaned = string_between('onload="check();">', '</body>', $data); return = stip_tags($cleaned, '<table><tr><td><div>'); } } any and all help will be greatly appreciated love to hear from you best regards I have this thing that i am trying to make but i cant get it to work.. can anyone help? Code: [Select] function sql_read( $dbname,$dbusername,$dbpassword ) { $names = array(); $password = array(); $connect = @mysql_connect("mysql11.000webhost.com",$dbusername,$dbpassword) or die("Could Not Connect"); @mysql_select_db ($dbname) or die("Could not find DataBase"); $query = mysql_query("select * from users"); $numrows = mysql_num_rows($query); if ($numrows > 0){ while($row = mysql_fetch_assoc($query)){ $names[] = $row["uname"]; $password[] = $row["password"]; $id = $row["id"]; } $return = array($names,$password,$id); }else{ $return = array(); } return $return[]; } $names = array(); $names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0]; The error i get is Code: [Select] Parse error: syntax error, unexpected '[' in /home/a5480952/public_html/sql/index.php on line 28 Line 28 is "$names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0];" Please help... i REALLLLD need help with this.. ask questions if you want to know more about what i am trying to do... thanks! I'm having troubling with trying to create a function to spit out a single array with the following array. I can write it in a away that looks through the arrays manually. the results i am trying to generate is that each item in generated array. Array to convert array( "account" => array( "login", "register", "logout", "edit", ), "p" => array( "report", ), "array1.0" => array( "array2.0" => array( "array3.0", "array3.1 ), "array2.1", ), generating the array will look like this
Array ( [0] => account [1] => account/login [2] => account/register [3] => account/logout [4] => account/edit [5] => p [6] => p/report [7] => array1.0 [8] => array1.0/array2.0 [9] => array1.0/array2.0/array3.0 [10] => array1.0/array2.0/array3.1 [11] => array1.0/array2.1 ) The idea is that id generates a single array with combined labels and arrays inside, etc. I just can't figure out how to create a script that will create this array even If I add a new value or array.
Hi guys; I've managed to find my way into something of a maze. <td> <a href="'Details/21/index.php'"?id= . $id .'>" . $row['id'] . "</a> </td> I'm looking at this line in the code. I realise it's currently in the wrong syntax but I'm just trying multiple different variations. At the moment I'm actually just trying to make it go to a static link but my real goal is to - get it to pick up the id number, of the id row I click, and concatenate that with the rest of my address string. Then use that as the href. Something like this: - "'Details/' + $id + '/index.php'" It's really confusing me though inside this loop and for some reason the id number is being picked up as an int, by the looks of things. It's getting above my level of understanding. Any chance one of you masters would through a n00b a lifeline?
This is my code below
<!DOCTYPE html> <html> <head> <title>LifeSaver DB</title> <h1> LifeSaver Database </h1> </head> <body> <table> <tr> <th>Id</th> <th>Location</th> <th>Initials</th> <th>TimeStamp</th> <th>Notes</th> </tr> <?php $conn = mysqli_connect("localhost", "meh", "pas", "DB"); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT * FROM LifeSaver1 ORDER BY id DESC"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { //for href row $id = $row['id']; $Footage = ['Footage']; echo "<tr> <td> <a href="'Details/21/index.php'"?id= . $id .'>" . $row['id'] . "</a> </td> <td>" . $row["Location"] . "</td> <td>" . $row["Initials"]. "</td> <td>" . $row["TimeStamp"]. "</td> <td>" . $row["Notes"] . "</td> </tr>";} //show table echo "</table>"; } else { echo "0 results"; } $conn->close(); ?> </table> </body> <style> table, td, th { border: 1px solid black; margin: auto; } table { border-collapse: collapse; color: #000; <!--font colour --> font-family: monospace; font-size: 18px; text-align: center;} th { background-color: #337AFF; color: white; font-weight: bold; } tr:nth-child(odd) {background-color: #add8e6} </style> </html> Edited February 8, 2020 by JonnyDriller I have two arrays, both with the same key values. I'd like to combine them. So for instance... Code: [Select] <?php $array1['abcd'] = array( 'value1' => "blah", 'value2' => "blahblah"); $array1['efgh'] = array( 'value1' => "ha", 'value2' => "haha", 'valuex' => "xyz"); $array2['abcd'] = array('value3' => "three", 'value4' => "four"); $array2['efgh'] = array( 'value3' => "hohoho", 'value6' => "six6"); function combine_arrays($array1,$array2) { //*combining* return $single_array; } echo "<pre>"; print_r(combine_arrays($array1,$array2)); echo "</pre>"; /* would produce ['abcd'] = ( 'value1' => "blah", 'value2' => "blahblah", 'value3' => "three", 'value4' => "four" ) ['efgh'] = ( 'value1' => "ha", 'value2' => "haha", 'valuex' => "xyz", 'value3' => "hohoho", 'value6' => "six6" ) */ ?> What's the easiest way to do this? How can i break down strings for example i have a simple search database for products where as a user searches a term this can be one, two three words and so on. I'm using a wildcard; but i want the best way to get the possible search so i would like to break down the string into separate words and then use that to search the database using wildcards is this possible? Thanks for reading. Hello, I was wondering if someone could help me to "get" the webaddress of a current webpage and then pull off pieces of the page. All of my pages look like: http://localhost:8888/algebra_book/Chapters/Quadratic_Functions/introductory_problem.php BUT, the stuff to the left of Quadratic Functions WILL change in the future. So all will look like: *Quadratic_Functions/introductory_problem.php Then, I'd like to pull off a $chapter variable, which in this case is Quadratic_Functions and a $page_name variable which in this case will be introductory_problem. Thanks so much. |