PHP - Simple Array Question
Hello.
I have a newbie question regarding arrays: How can I echo an element's subsequent arrays and elements? For example Code: [Select] $pers = array("Steve" => array("eyes" => "green", "age" => "43", "race" => "caucasian")); and if I echo $pers["Steve"] to make it display: green 43 caucasian. Thanks. Similar TutorialsI will state right here that I've never really delved into PHP before, but I'm prying apart the code on my site (a programmer made it for me). I've figured out how to add more checkboxes onto the page, but I've run into a problem. Code: [Select] 'Artist' => array ( 'Cassiadawn', 'Cryptic', 'Diction', 'Glorfindel', 'Kyrislian', 'Russa', 'Ntkufreak', 'Taliba', 'Thunderbun', 'Velg', 'Lostdollie', 'Pookawitch') What I'm trying to do is replace 'Lostdollie', 'Pookawitch' with 'Lostdollie [R]', 'Pookawitch[R]'. How can I do this? I'd really appreciate any help anyone can give me! Hey, Can someone please fix my code below? I simply want to add all values for $row[score1] in first if statement and add all values for $row[score2] in second if statement and then add them together to return final value . Code: [Select] while( $row = mysql_fetch_assoc($query)) { if($clanID==$row[clan1] && $row[score1] > $row[score2]) { $points[] = $row[score1]; } if($clanID==$row[clan2] && $row[score1] < $row[score2]) { $points[] = $row[score2]; } } return $points;. So lets say I echo the variables that I want added... Code: [Select] if($clanID==$row[clan1] && $row[score1] > $row[score2]) { $points[] = $row[score1]; echo $row['score1']."<br>"; } if($clanID==$row[clan2] && $row[score1] < $row[score2]) { $points[] = $row[score2]; echo $row['score2']."<br>"; } On page it returns as: 11 16 14 Add it together is 41 and this is what I want the output to be. Hope you understand I appreciate any help! P.S. I must be able to return final value OUTSIDE loop. Thanks. Hey guys I have an array and want to make it into a sting and separate each array segment by a comma...is there a function for this or should I just do it in a loop myself?...thanks my php array knowledge is still basic and i can't seem to get this code to yield an answer in the form i want. i would like to be able to get at $m and $b separately so I can manipulate them. how, for example do i just print $m? Code: [Select] <?php var_dump( linear_regression(array(1, 2, 3, 4), array(1.5, 1.6, 2.1, 3.0)) ); /** * linear regression function * @param $x array x-coords * @param $y array y-coords * @returns array() m=>slope, b=>intercept */ function linear_regression($x, $y) { // calculate number points $n = count($x); // ensure both arrays of points are the same size if ($n != count($y)) { trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR); } // calculate sums $x_sum = array_sum($x); $y_sum = array_sum($y); $xx_sum = 0; $xy_sum = 0; for($i = 0; $i < $n; $i++) { $xy_sum+=($x[$i]*$y[$i]); $xx_sum+=($x[$i]*$x[$i]); } // calculate slope $m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum)); // calculate intercept $b = ($y_sum - ($m * $x_sum)) / $n; return array("m"=>$m, "b"=>$b); } ?> Ok guys and gals I'm gettin' old! I know this is simple but I've spent way too much time on this now and am crying 'uncle'! I'm trying load a multi-dementional array with mysql_fetch_array results but I'm not getting it. Here's the rough basis for what I'm trying to do: $result = mysql_query("SELECT * FROM phpbb_profile_fields_data"); $row = mysql_fetch_array($result) or die(mysql_error()); $some_array = array( array( 'member_name' => $row['pf_firstname'] . " " . $row['pf_lastname'], 'member_title' => $row['pf_title'], 'member_employer' => $row['pf_employer'], ), array( 'member_name' => 'Janet', 'member_title' => '47', 'member_employer' => 'Husband', ), ); Okay, so that loads two $some_array elements with data. The first element has the first row of the database in question. Works fine but useless. So, with a "while ($row = mysql_fetch_array($result) )" what do I need to do to fill the $some_array with the query results? Thanks! Hi there, I have the following PHP code: <?php $books = array( array("title"=>"Book Title 1", "author"=>"Author 1", "publisher"=>"Publisher 1"), array("title"=>"Book Title 2", "author"=>"Author 2", "publisher"=>"Publisher 2"), array("title"=>"Book Title 3", "author"=>"Author 3", "publisher"=>"Publisher 3"), ); foreach ($books as $key => $value) { echo "$key : $value<br>"; } ?> I'm trying to loop through this code, but all it outputs is: 0: ARRAY 1: ARRAY 2: ARRAY -- It's not even pulling the proper $keys and $values. It works when there's just ONE array -- but not with this multidimensional array. Any ideas? Thanks! J if(in_array("b", $array)) { } The above checks to see if a value is present in an array, but how do I then retrieve the array key value from the matching value? $array[0] = "a"; $array[1] = "b"; $array[2] = "c"; So in the example, the answer is 1 (array key 1). Trying to do something that I think should be very simple. Let's assume that I have an array populated with the numbers 1 to 24 for this example. I need to output these items in 4 columns to accomodate my CSS layout but still have them read from left to right, top to bottom. I also want this to be somewhat dynamic - I'm okay hardcoding it to split into 4 columns, but need it to handle any number of items in the array. I'm not sure if the best way to do this would be to split the original array into an array for each of the 4 columns, or to loop through the original array 4 times and output items 1+4, 2+4, and so on. Or maybe I should be using array_filter with some custom callback functions. I'm fine with any solution. The end result is echoing the array in the following groupings so I can surround each column with the appropriate column HTML for layout (in this case the array actually contains IMG urls): Column 1: 1, 5, 9, 13, 17, 21 Column 2: 2, 6, 10, 14, 18, 22 Column 3: 3, 7, 11, 15, 19, 23 Column 4: 4, 8, 12, 16, 20, 24 Any help would be appreciated! This one is driving me nuts and I know it should be relatively easier. Guess I should stick to HTML + CSS. Hi guys, this function below is for card validation, however anycard i enter i get the invalid card number, this is not connected to any gateways, this is just an Luhn card validation demonstration. can you tell me please why i get that? <?php if ($_POST['pay']) { $cardNo = addslashes(strip_tags($_POST['cardNo'])); function luhnCheck($cardNo) { $cardNo = str_replace(" ", "", trim($cardNo)); if(!is_numeric($cardNo) or strlen($cardNo)>19) return false; $skip = $newNum = $total = 0; for($i=0;$i<=(strlen($cardNo)-1);$i++) { if($skip ==0) { $tmpNum = ($cardNo[$i]*2); $total += (strlen($tmpNum)==2 ? (substr($tmpNum, 0, 1)+substr($tmpNum, 1)) : $tmpNum); $skip = 1; }else{ $total += $cardNo[$i]; $skip = 0; } } return (($total % 10) == 0); } /* Example Usage */ if(luhnCheck($_GET['cc'])) { echo("Valid Number."); }else{ echo("Invalid Number."); } } ?> <form name="confirm" method="post" action=""> <input type="text" name="cardNo"> <input type="submit" name="pay" value="Pay Now"> </form> I know it could be a issue with this line if(luhnCheck($_GET['cc'])) { but im wondering where should and how put this cc in my form? regards Hello to everyone I would like to ask for this simple question.. Im trying to put if and else condition on my php code. However this condition is not working.. Code: [Select] if($file == #3#){ echo "No results found"; }else{ echo "results has been found"; } This variable $file is the result after i used an API.. If the API does not see any results they would return #3# as the result. This means they returned a transaction code of 3 which you can see here http://developer.textapp.net/WebService/TransactionCodes.aspx means that there was nothing to return. The condition does not function well even if i make 3 or #3# as part of the condition. Can you help me guys? thanks what does the # symbol do in php(e.x index.php?cat=1#privacy)? How can i use it? From what i 've seen you use it for changing a whole div without reloading the page like tabs.. Hello everyone! I am a new user and I have a simple question. How do I set a password for a user to log in to my site with PHP? I have no MySQL database just a iOS code/host app. I know this isn’t secure but only me and my friends are on the site. Any help is appreciated. hello all I'm VERY new to php and I just have a simple question how do I bold the output text of this string echo $row['name']."<BR>"; Hey! I have a drop down field for my "register" form, but this is clearly easy to edit, I just used tamperdata for firefox and yeah...pretty easy, is there any way that you could prevent tampering with the values when submitting this form!? I'm teaching myself php and mysql -- and my coding is a little rough. But what i'm trying to do is display information from a mysql table in another table on a website. I can do this just fine simply displaying the table, but i want a little something different. I am only displaying one column of the table, instead of displaying it all in one column, i want to display the information in 5 columns. for example..... the table from mysql has names scott, john, james, mary, simon. with the code I have i can make it display like this: scott scott scott scott scott john john john john john james james james james james mary mary mary mary mary simon simon simon simon simon i want it to diplay like this: scott john james mary simon new names will continue down here is my code: <?php require ("connectigb.php"); $result = mysql_query ("SELECT pledgeName FROM pledgewall"); $fields_num = mysql_num_fields($result); echo "<center> <h1>TSA It Gets Better Pledge Wall</h1>"; echo "<table border='1' align='center' style='width:960px;'><tr>"; while($row = mysql_fetch_row($result)) { echo "<tr>"; // $row is array... foreach( .. ) puts every element // of $row to $cell variable foreach($row as $cell) if ($cell != "") { for ($j=0; $j<5; $j++) { echo "<td>$cell</td>"; } } echo "</tr>\n"; } mysql_free_result($result); echo "</table></center>"; ?> any ideas on what i can do here, i'm sure this is a simply fix. i just haven't come across it yet. thanks for the help in advance. if someone can help me out with this, its quite simple but I can't seem to get it to work.. Basically, this is the code I have for now, the issue is if I have 0 product or items, I don't want the sorting option to show up on the page, from the code below, what line can I add tosay that if there is no products in a category, not to show the SORT feature. thanks in advance if anyone can help. {if isset($orderby) AND isset($orderway)} <!-- Sort products --> {if isset($smarty.get.id_category) && $smarty.get.id_category} {assign var='request' value=$link->getPaginationLink('category', $category, false, true)} {elseif isset($smarty.get.id_manufacturer) && $smarty.get.id_manufacturer} {assign var='request' value=$link->getPaginationLink('manufacturer', $manufacturer, false, true)} {elseif isset($smarty.get.id_supplier) && $smarty.get.id_supplier} {assign var='request' value=$link->getPaginationLink('supplier', $supplier, false, true)} {else} {assign var='request' value=$link->getPaginationLink(false, false, false, true)} {/if} <form id="productsSortForm" action="{$request|escape:'htmlall':'UTF-8'}"> <select id="selectPrductSort" onchange="document.location.href = $(this).val();"> <option value="{$link->addSortDetails($request, $orderbydefault, $orderwaydefault)|escape:'htmlall':'UTF-8'}" {if $orderby eq $orderbydefault}selected="selected"{/if}>{l s='--'}</option> {if !$PS_CATALOG_MODE} <option value="{$link->addSortDetails($request, 'price', 'asc')|escape:'htmlall':'UTF-8'}" {if $orderby eq 'price' AND $orderway eq 'asc'}selected="selected"{/if}>{l s='Price: lowest first'}</option> <option value="{$link->addSortDetails($request, 'price', 'desc')|escape:'htmlall':'UTF-8'}" {if $orderby eq 'price' AND $orderway eq 'desc'}selected="selected"{/if}>{l s='Price: highest first'}</option> {/if} <option value="{$link->addSortDetails($request, 'name', 'asc')|escape:'htmlall':'UTF-8'}" {if $orderby eq 'name' AND $orderway eq 'asc'}selected="selected"{/if}>{l s='Product Name: A to Z'}</option> <option value="{$link->addSortDetails($request, 'name', 'desc')|escape:'htmlall':'UTF-8'}" {if $orderby eq 'name' AND $orderway eq 'desc'}selected="selected"{/if}>{l s='Product Name: Z to A'}</option> {if !$PS_CATALOG_MODE} <option value="{$link->addSortDetails($request, 'quantity', 'desc')|escape:'htmlall':'UTF-8'}" {if $orderby eq 'quantity' AND $orderway eq 'desc'}selected="selected"{/if}>{l s='In-stock first'}</option> {/if} </select> <label for="selectPrductSort">{l s='Sort by'}</label> </form> <!-- /Sort products --> {/if} I can't figure out why im getting an error with this code. if ($notif = mysql_query("SELECT * FROM notifications_1 WHERE recieverid='". $_SESSION['id']."' ORDER BY read ASC, id DESC") or die (mysql_error())); { Code: [Select] 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 'read ASC, id DESC' at line 1 Hello there fellow programmers, I was just wondering if there was any difference between doing this: Code: [Select] echo "Say Something Here"; and this Code: [Select] echo "Say "; echo "Something "; echo "Here"; Thank you in advance. Notice: I am aware of the fact that they will output the same thing, my question is regarding resources. Hi. I have an ID in md5 format but in the database its not. I pass the md5 ID to a page and then I am trying to load the correct record via a look up in the database of that ID. Im wondering is this code possible? <?php $query = mysql_query("SELECT * FROM table WHERE ".md5(tableID)." = $ID"); ?> Thanks Lets say I have: $var=5; What would an output look like to get me: 1<br> 2<br> 3<br> 4<br> 5<br> Im at a complete loss! Thanks! |