PHP - String Offset As An Array Error
I am writing a data scraping script but i am getting fatal error.Can someone please help me.
My code:- Code: [Select] <?php $con = mysql_connect("localhost","root",""); mysql_select_db("cakeshop", $con); $test = '<form name="select"> <p><font color="#000000" size="3"><select name="city" size="1" onchange="self.location.href=this.options[this.selectedIndex].value"> <option value="xxxwi.htm">Wisconsin</option> <option value="xxxwy.htm">Wyoming</option> </select><br clear="all"> </font><font color="#000000" size="2">Click on the state below where you want to send cake<br> or use the pull down menu above </font> </form> '; preg_match_all('~<option\s+value="(.*?)">(.*?)</option>~', $test, $out); for($i=0;$i<=2;$i++) { $data = file_get_contents('http://xxx.com/'.$out[1][$i].''); preg_match_all('~<option\s+value="(.*?)">(.*?)</option>~', $data, $city); //$klimit = sizeof($city); $klimit = count($city[1]); for($k=2 ;$k < $klimit; $k++) { $data = file_get_contents('http://xxx.com/'.$city[1][$k].''); //Error on this line $regex = '~<td\s+colspan="2"\s+width="350"><font\s+size="2">\s+<b>\s+(.*?) <\/b><br>(.*?) <br>(.*?),\s+(.*?)\s+<br>(.*?), (.*?)\s+<BR><BR><font\s+size="2"><img\s+src="\.\.\/images\/phone1.gif"\s+align="left"\s+hspace="4"\s+alt\s+=(.*)>\s+-\s+Phone\s+#\s+(.*?)\s+<\/font>\s+<BR>\s+<font\s+size\s+="1">~'; preg_match_all($regex, $data, $final); $jlimit = count($final[1]); for($j=0 ;$j < $jlimit; $j++) { $name = $final[1][$j]; $name = mysql_real_escape_string($name); $address = $final[2][$j]; $address = mysql_real_escape_string($address); $city = $final[3][$j]; $city = mysql_real_escape_string($city); $state = $final[4][$j]; $state = mysql_real_escape_string($state); $pincode = $final[6][$j]; $pincode = mysql_real_escape_string($pincode); $telephone = $final[8][$j]; $telephone = mysql_real_escape_string($telephone); mysql_query("INSERT INTO cakeshop (name, address, city, state, pincode, telephone) VALUES ('$name', '$address', '$city', '$state', '$pincode', '$telephone')"); } } } mysql_close($con); echo "Data scraping completed"; ?> Error message :- Code: [Select] Fatal error: Cannot use string offset as an array in C:\Users\Booone\AppData\Roaming\NuSphere\PhpED\projects\scraping1.php Error is on this line "$data = file_get_contents('http://xxx.com/'.$city[1][$k].'');" Similar TutorialsHi, when I run my script I get the following error message. These are array variables I am passing into the playerAttack function. I can't figure out what is wrong it looks fine to me. Any help greatly appreciated. Thanks. Derek Code: [Select] attack posted Fatal error: Cannot use string offset as an array in C:\wamp\www\SUN_DRAGON_GAME\gamestart.php on line 202 Code: [Select] $currentMonster='octalisk'; ///////////////////////////////////////////////////////////battle//////////////////////////// //function monsterEncounter($echoMonster,$monsterImage) if(!empty($_POST['attack'])) { echo "attack posted"; playerAttack($currentMonster,$player['stats']['playerHitRollLow'],$player['stats']['playerHitRollHigh'], $player['stats']['playerDmgLow'],$player['stats']['playerDmgHigh'],$monsters['Ocean']['octalisk']['defense'],$monster['Ocean']['octalisk']['hp'],$player['faction']['Teardrop_Ocean_Protectors']); $doesItAttack=encounter($monsters['ocean']['octalisk']['seeInvis'],$monsters['ocean']['octalisk']['aggro'],$player['factions']['Teardrop_Ocean_Protectors']); //insert octalisk stats into monsterattack function. if($doesItAttack =='yes') { monsterAttack($currentMonster,$monsters['Ocean']['octalisk']['hitRollLow'],$monsters['Ocean']['octalisk']['hitRollHigh'] , $monsters['Ocean']['octalisk']['dmgLow'],$monsters['Ocean']['octalisk']['dmgHigh'], $player['stats']['playerDefense'],$player['stats']['playerHp'],$monsters['Ocean']['octalisk']['hp']); } else { echo "do nothing"; } } here is the attack function Code: [Select] function playerAttack($currentMonster,$hitRollLow,$hitRollHigh,$dmgLow,$dmgHigh,$monsterDefense, $monsterHp,$playerFaction) { echo "in player attack"; $playerRoll= rand($playerHitRollLow, $playerHitRollHigh); if($playerHitRoll>=$monsterDefense) { global $theMessage; //shit had to make it global $playerDamage= rand($playerDmgLow,$playerDmgHigh); $theMessage = "You have hit a ". $currentMonster." for "."<strong>". $playerDamage."</strong> points of damage!"; $monsterHp= $monsterHp- $playerDamage; $theMessage.=" The ".$currentMonster."'s hp is now ".$monsterHp; if($monsterHp<=0) { if($playerFaction<=0) { $theMessage="Your faction standing with ".$playerFaction. "could not possibly get any worse."; } } $theMessage.=" You have killed the ".$currentMonster. "Your faction standing with ".$playerFaction." is now ".$playerFaction-1; } else { global $theMessage; //WTF i dont know what im doing. $theMessage= " Whoa! You missed the ".$currentMonster; } } Hello, I have this script that I have been running a long time that is included in my site with a php include. It reads data from a cached file in an array. I noticed last night something has happened and it won't load in my site anymore and causes my site not to load. I determined this through various troubleshooting. Through the various troubleshooting I was able to get an error "Fatal error: Cannot use string offset as an array in /home4/mesquiu0/public_html/nws_alerts_scroller.php on line 44". This is strange because nothing has changed on my server or with this script. Also if I run it by itself it runs fine. http://www.mesquitew...ts_scroller.php but, when I include it in my site it won't load and I get the error. I am not sure what is causing this. I have done some research and it appears to be cause by a key as though it were an array of arrays. I just don't see it though.
Here is the portion of the code it is referring to.
// IF there are alerts if(!empty($atomAlerts)) { // IF there are alerts usort($atomAlerts, 'a_sort'); // sort locations foreach($atomAlerts as $aak => $aav) { // FOR EACH alert, get the data from the cache file $caav = count($aav); // count alerts for($i=0;$i<$caav;$i++) { // FOR EACH alert // assemble alert data $title[$i][0] = '<b>'.strtoupper($aav[$i][0]).'</b>'; @$adarray[$aav[$i][10].' '.$title[$i][0]] .= ' - <a href="'.$alertURL.'?a=' .$aav[$i][14].'#WA'.$aav[$i][13].'" title=" Details for '.$aav[$i][12] .' '.$aav[$i][0].'">' .$aav[$i][12] ."</a>"; } } }Here is a sample of the cached file the script reads from. $atomAlerts = array ( 'TXZ104' => array ( 0 => array ( 0 => 'Dense Fog Advisory', 1 => 'Expected', 2 => 'Minor', 3 => 'Likely', 4 => 1418136720, 5 => 1418148000, 6 => 'Anderson; Bell; Bosque; Collin; Comanche; Cooke; Coryell; Dallas; Delta; Denton; Eastland; Ellis; Erath; Falls; Fannin; Freestone; Grayson; Hamilton; Henderson; Hill; Hood; Hopkins; Hunt; Jack; Johnson; Kaufman; Lamar; Lampasas; Leon; Limestone; McLennan; Milam; Mills; Montague; Navarro; Palo Pinto; Parker; Rains; Robertson; Rockwall; Somervell; Stephens; Tarrant; Van Zandt; Wise; Young', 7 => 'DENSE FOG IS OCCURRING OVER THE REGION WHICH WILL CREATE VERY HAZARDOUS DRIVING CONDITIONS. SLOW DOWN...USE YOUR LOW BEAM HEADLIGHTS...AND LEAVE PLENTY OF DISTANCE BETWEEN YOU AND THE VEHICLE AHEAD OF YOU.', 8 => '...DENSE FOG ADVISORY NOW IN EFFECT UNTIL NOON CST TODAY... * LOCATION...ALL OF NORTH CENTRAL TEXAS. * VISIBILITY...WIDESPREAD VISIBILITY REDUCTIONS OF LESS THAN 1/4 OF A MILE. VISIBILITIES WILL BEGIN TO IMPROVE AFTER NOON. * IMPACTS...DENSE FOG WILL MAKE DRIVING HAZARDOUS.', 9 => '#F60', 10 => ' <img src="./images/FGY.gif" width="12" height="12" alt="Dense Fog Advisory" title=" Dense Fog Advisory" />', 11 => '56', 12 => 'Collin Co', 13 => '1', 14 => 'TXZ104', 15 => '', ), ),Any idea what might be causing this. I don't see anything right off the bat. Just strange it loads fine alone and the problem only happens and happened out of the blue when I include it in my site. I have tried to isolate it to see what might be conflicting with it but have been unsuccessful. I also would add it only errors and doesn't load when included in my site when there is an alert. Right now there is no alerts at the moment. There was alerts at the time this happened. -Thanks Edited by Texan78, 09 December 2014 - 02:19 PM. Hi, I know the problem that I am having is related to the change in PHP5 about strings and arrays, however I do not understand why my script is affected. It seems like I am not trying to add more variables to an array. I do not success in order solve the problem. Help me please: // Build View Array if ($view_result!=false) { $i=0; while ($view_row =@ mysql_fetch_array($view_result)){ $i++; $view_rows[$i] = $view_row; // build view_comments array if ($viewcomments=="yes") { $sql = "SELECT * FROM blog_comments WHERE nBlogId=".$view_row["nIdCode"]." ORDER BY nIdCode DESC"; $result = mysql_query($sql,$con); if ($result!=false){ $j=0; while ($comment_row =@ mysql_fetch_array($result)){ $j++; $view_comment_rows[$i][$j] = $comment_row; } } } } } else { $problem = "2"; } // increment view counter for either the single blog being shown // or ALL blogs being shown //The problem is here if ($view_rows[1]["nIdCode"]!="" && strpos($_SERVER["PHP_SELF"],"admin.php")==false){ if ($viewmode=="single"){ $blogid = $view_rows[1]["nIdCode"]; $sql = "UPDATE blog SET nViews=nViews+1 WHERE nIdCode=".$blogid; } else { for ($i=1;$i<=count($view_rows);$i++){ $blog_array[$i] = $view_rows[$i]["nIdCode"]; } $blogids = implode(",",$blog_array); $sql = "UPDATE blog SET nViews=nViews+1 WHERE nIdCode IN (".$blogids.")"; } $result = mysql_query($sql,$con); if ($result==false){ print "<li>Problem with SQL<br>[".$sql."]</li>\n"; } } } Last edited by sbarros (2011-01-08 18:21:52) My code Code: [Select] <?php set_time_limit (0); include("config.php"); $data = "<td><a href='/hospitals-in/Alabama'>Alabama</a></td> <td><a href='/hospitals-in/District-of-Columbia'>District of Columbia</a></td> <td><a href='/hospitals-in/Kentucky'>Kentucky</a></td> <td><a href='/hospitals-in/Montana'>Montana</a></td> <td><a href='/hospitals-in/Ohio'>Ohio</a></td> <td><a href='/hospitals-in/Texas'>Texas</a></td> <td><a href='/hospitals-in/Alaska'>Alaska</a></td> <td><a href='/hospitals-in/Florida'>Florida</a></td> <td><a href='/hospitals-in/Louisiana'>Louisiana</a></td> <td><a href='/hospitals-in/Nebraska'>Nebraska</a></td> <td><a href='/hospitals-in/Oklahoma'>Oklahoma</a></td> <td><a href='/hospitals-in/Utah'>Utah</a></td> <td><a href='/hospitals-in/America-Samoa'>America Samoa</a></td> <td><a href='/hospitals-in/Georgia'>Georgia</a></td> <td><a href='/hospitals-in/Maine'>Maine</a></td> <td><a href='/hospitals-in/Nevada'>Nevada</a></td> <td><a href='/hospitals-in/Oregon'>Oregon</a></td> <td><a href='/hospitals-in/Vermont'>Vermont</a></td> <td><a href='/hospitals-in/Arizona'>Arizona</a></td> <td><a href='/hospitals-in/Hawaii'>Hawaii</a></td> <td><a href='/hospitals-in/Maryland'>Maryland</a></td> <td><a href='/hospitals-in/New-Hampshire'>New Hampshire</a></td> <td><a href='/hospitals-in/Pennsylvania'>Pennsylvania</a></td> <td><a href='/hospitals-in/Virgin-Islands'>Virgin Islands</a></td> <td><a href='/hospitals-in/Arkansas'>Arkansas</a></td> <td><a href='/hospitals-in/Idaho'>Idaho</a></td> <td><a href='/hospitals-in/Massachusetts'>Massachusetts</a></td> <td><a href='/hospitals-in/New-Jersey'>New Jersey</a></td> <td><a href='/hospitals-in/Puerto-Rico'>Puerto Rico</a></td> <td><a href='/hospitals-in/Virginia'>Virginia</a></td> <td><a href='/hospitals-in/California'>California</a></td> <td><a href='/hospitals-in/Illinois'>Illinois</a></td> <td><a href='/hospitals-in/Michigan'>Michigan</a></td> <td><a href='/hospitals-in/New-Mexico'>New Mexico</a></td> <td><a href='/hospitals-in/Rhode-Island'>Rhode Island</a></td> <td><a href='/hospitals-in/Washington'>Washington</a></td> <td><a href='/hospitals-in/Colorado'>Colorado</a></td> <td><a href='/hospitals-in/Indiana'>Indiana</a></td> <td><a href='/hospitals-in/Minnesota'>Minnesota</a></td> <td><a href='/hospitals-in/New-York'>New York</a></td> <td><a href='/hospitals-in/South-Carolina'>South Carolina</a></td> <td><a href='/hospitals-in/West-Virginia'>West Virginia</a></td> <td><a href='/hospitals-in/Connecticut'>Connecticut</a></td> <td><a href='/hospitals-in/Iowa'>Iowa</a></td> <td><a href='/hospitals-in/Mississippi'>Mississippi</a></td> <td><a href='/hospitals-in/North-Carolina'>North Carolina</a></td> <td><a href='/hospitals-in/South-Dakota'>South Dakota</a></td> <td><a href='/hospitals-in/Wisconsin'>Wisconsin</a></td> <td><a href='/hospitals-in/Delaware'>Delaware</a></td> <td><a href='/hospitals-in/Kansas'>Kansas</a></td> <td><a href='/hospitals-in/Missouri'>Missouri</a></td> <td><a href='/hospitals-in/North-Dakota'>North Dakota</a></td> <td><a href='/hospitals-in/Tennessee'>Tennessee</a></td> <td><a href='/hospitals-in/Wyoming'>Wyoming</a></td>"; preg_match_all("~<td><a\s+href='(.*?)'>(.*?)</a></td>~", $data, $link); echo '<pre>'; echo print_r($link); echo '</pre>' ; $countlink = count($link[1]); for($i=0 ; $i < $countlink; $i++) { if (is_array($link)) { $sitelink = "http://www.xxx.com".$link[1][$i]; $hospitallink = file_get_contents("$sitelink"); preg_match_all("~<td><a href=\"/hospital/(.*?)\">(.*?)</a></td>~", $hospitallink, $hospitalinfo); $city = $link[2][$i]; $countinfo = count($hospitalinfo[1]); for($j=0 ; $j < $countinfo ; $j++) { $infolink = "http://www.xxx.com/hospital/".$hospitalinfo[1][$j]; $getinfo = file_get_contents("$infolink"); $regex = "~<b>Name:</b>\s+(.*?)\s+</p>\s+<p>\s+<b>Address:</b>\s+(.*?)\s+</p>\s+<p>\s+<b>Phone:</b>\s+(.*?)\s+</p>\s+<p>\s+<b>Number\s+of\s+Beds:</b>\s+(.*?)\s+</p>\s+<p>\s+<b>Type:</b>\s+(.*?)\s+</p>\s+<p>\s+<b>System:</b>\s+(.*?)\s+</p>\s+<p>\s+<b>Website:</b>\s+<a href=\"(.*?)\">(.*?)</a>\s+</p>\s+<p>\s+(.*?)</p>~s"; preg_match_all($regex, $getinfo, $critinfo); preg_match_all("~<li>\s+(.*?)</li>~s", $getinfo, $servinfo); preg_match_all("~<h4>Hospital\s+Quality\s+and\s+Rating\s+information</h4>\s+<p>Data\s+based\s+on\s+2010\s+Health\s+Quality\s+Alliance\s+database</p>\s+<p>\s+<b>(.*?)</b><br>\s+(.*?)</p>~s", $getinfo, $mortality); preg_match_all("/(\d+)?\.?(\d+)?%/", $mortality[2][0], $number); $name = $critinfo[1][0]; $address = preg_replace('/\s\s+/', ' ', $address); $number = implode(",", $number[0]); $servinfo = implode(",", $servinfo[1]); $sql = "INSERT INTO hospital (name, address, servinfo, mortality) VALUES ('$name', '$address', '$servinfo', '$number')"; $exec_sql = mysql_query($sql); } } } ?> print_r($link) shows this :- Code: [Select] Array ( [0] => Array ( [0] => Alabama [1] => District of Columbia [2] => Kentucky [3] => Montana [4] => Ohio [5] => Texas [6] => Alaska [7] => Florida [8] => Louisiana [9] => Nebraska [10] => Oklahoma [11] => Utah [12] => America Samoa [13] => Georgia [14] => Maine [15] => Nevada [16] => Oregon [17] => Vermont [18] => Arizona [19] => Hawaii [20] => Maryland [21] => New Hampshire [22] => Pennsylvania [23] => Virgin Islands [24] => Arkansas [25] => Idaho [26] => Massachusetts [27] => New Jersey [28] => Puerto Rico [29] => Virginia [30] => California [31] => Illinois [32] => Michigan [33] => New Mexico [34] => Rhode Island [35] => Washington [36] => Colorado [37] => Indiana [38] => Minnesota [39] => New York [40] => South Carolina [41] => West Virginia [42] => Connecticut [43] => Iowa [44] => Mississippi [45] => North Carolina [46] => South Dakota [47] => Wisconsin [48] => Delaware [49] => Kansas [50] => Missouri [51] => North Dakota [52] => Tennessee [53] => Wyoming ) [1] => Array ( [0] => /hospitals-in/Alabama [1] => /hospitals-in/District-of-Columbia [2] => /hospitals-in/Kentucky [3] => /hospitals-in/Montana [4] => /hospitals-in/Ohio [5] => /hospitals-in/Texas [6] => /hospitals-in/Alaska [7] => /hospitals-in/Florida [8] => /hospitals-in/Louisiana [9] => /hospitals-in/Nebraska [10] => /hospitals-in/Oklahoma [11] => /hospitals-in/Utah [12] => /hospitals-in/America-Samoa [13] => /hospitals-in/Georgia [14] => /hospitals-in/Maine [15] => /hospitals-in/Nevada [16] => /hospitals-in/Oregon [17] => /hospitals-in/Vermont [18] => /hospitals-in/Arizona [19] => /hospitals-in/Hawaii [20] => /hospitals-in/Maryland [21] => /hospitals-in/New-Hampshire [22] => /hospitals-in/Pennsylvania [23] => /hospitals-in/Virgin-Islands [24] => /hospitals-in/Arkansas [25] => /hospitals-in/Idaho [26] => /hospitals-in/Massachusetts [27] => /hospitals-in/New-Jersey [28] => /hospitals-in/Puerto-Rico [29] => /hospitals-in/Virginia [30] => /hospitals-in/California [31] => /hospitals-in/Illinois [32] => /hospitals-in/Michigan [33] => /hospitals-in/New-Mexico [34] => /hospitals-in/Rhode-Island [35] => /hospitals-in/Washington [36] => /hospitals-in/Colorado [37] => /hospitals-in/Indiana [38] => /hospitals-in/Minnesota [39] => /hospitals-in/New-York [40] => /hospitals-in/South-Carolina [41] => /hospitals-in/West-Virginia [42] => /hospitals-in/Connecticut [43] => /hospitals-in/Iowa [44] => /hospitals-in/Mississippi [45] => /hospitals-in/North-Carolina [46] => /hospitals-in/South-Dakota [47] => /hospitals-in/Wisconsin [48] => /hospitals-in/Delaware [49] => /hospitals-in/Kansas [50] => /hospitals-in/Missouri [51] => /hospitals-in/North-Dakota [52] => /hospitals-in/Tennessee [53] => /hospitals-in/Wyoming ) [2] => Array ( [0] => Alabama [1] => District of Columbia [2] => Kentucky [3] => Montana [4] => Ohio [5] => Texas [6] => Alaska [7] => Florida [8] => Louisiana [9] => Nebraska [10] => Oklahoma [11] => Utah [12] => America Samoa [13] => Georgia [14] => Maine [15] => Nevada [16] => Oregon [17] => Vermont [18] => Arizona [19] => Hawaii [20] => Maryland [21] => New Hampshire [22] => Pennsylvania [23] => Virgin Islands [24] => Arkansas [25] => Idaho [26] => Massachusetts [27] => New Jersey [28] => Puerto Rico [29] => Virginia [30] => California [31] => Illinois [32] => Michigan [33] => New Mexico [34] => Rhode Island [35] => Washington [36] => Colorado [37] => Indiana [38] => Minnesota [39] => New York [40] => South Carolina [41] => West Virginia [42] => Connecticut [43] => Iowa [44] => Mississippi [45] => North Carolina [46] => South Dakota [47] => Wisconsin [48] => Delaware [49] => Kansas [50] => Missouri [51] => North Dakota [52] => Tennessee [53] => Wyoming ) ) 1 Earlier it was giving an error without if (is_array($link)) { Code: [Select] Fatal error: Cannot use string offset as an array However after adding it, it wont go to /hospitals-in/District-of-Columbia which is $link[1][1]; Can someone please point me my mistake I'm getting an error: Undefined offset: 1 in… I'm trying to display a 2D array after filling it. It displays fine if I echo it while it's being made, but after it is not working.
I'm making a pyramid solitaire game and am trying to set up the pyramid of cards. There are 7 arrays, each with a set amount of cards in them. The first array only gets one cards while the last array gets seven. This makes it like a pyramid. All the indexes that don't get a card are set to 0. So it looks like this where the 1's are cards:
1 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0
1 1 1 0 0 0 0 0
1 1 1 1 0 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 1 0 0
1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1
The display2Darray function is what is causing the error. Specifically this line:
if($array[$i][$j] == 0)Heres my main code: <?php include_once "pyramidCard.php"; include_once "pyramidDeck.php"; $theDeck = new Deck(); $pyramid = array(array(), array(), array(), array(), array(), array(), array()); $size = 7; $theDeck->shuffleCards(); makePyramid($pyramid, $size, $theDeck); display2Darray($pyramid, $size); //************************ // FRUNCTIONS * //************************ function makePyramid($array, $size, $deck) { $row = 0; for($i = 0; $i < $size; $i++) { for($j = 0; $j < $size; $j++) { if($j > $row) { $array[$i][$j] = 0; //echo ". <br>"; } else { $array[$i][$j] = $deck->dealCards(); //echo "this card is ".$array[$i][$j]->getFace()." of ".$array[$i][$j]->getSuit()."<br>"; } } $row++; } } function display2Darray($array, $size) { for($i = 0; $i < $size; $i++) { for($j = 0; $j < $size; $j++) { if($array[$i][$j] == 0) echo " "; else echo $array[$i][$j]->getFace()." of ".$array[$i][$j]->getSuit(); } echo "<br>"; } } ?> Hi, WHAT: So I cannot for the life of me figure out why I'm getting this error. I'm trying to create a function that will display the user information from the database in a table but I have like a ton of these errors on the page and I don't know whats wrong. STEPS TO RESOLVE: So I've gone over my code a bunch of types to make sure that all the variables and what not were spelled correctly and as far as I can tell they are. I've also googled this issue to see if I can find a solution but none of them are very helpful. I honestly don't know whats wrong so kinda hard to find ways to resolve an issue, I don't even really know what this error means. THE CODE: This is where I put the function into action <?php display_table( array( "id" => "Id", "emailaddress" => "Email", "firstname" => "First Name", "lastname" => "Last Name", "salesperson" => "Salesperson", "phonenumber" => "Phone Number", "extension" => "Extension", "type" => "Type" ) ); ?> //This is the function <?php function display_table($fields, $data, $rows, $page){ if(isset($_GET['page'])){ $page = $_GET['page']; } else { $page = 1; } $firstRecord = ($page - 1) * ROWS_PER_PAGE; $pageNumbers = ceil($rows / ROWS_PER_PAGE); echo '<div class="table-responsive w-75 mx-auto py-3"> <table class="table table-dark table-bordered table-sm"> <thead> <tr>'; foreach($fields as $key){ echo '<th class="py-2">' . $key . '</th>'; } echo '</tr> </thead> </tbody>'; $keys = array_keys($fields); for($record = $firstRecord; $record < $firstRecord + ROWS_PER_PAGE; $record++){ $row = $data[$record]; echo '<tr>'; for($recordCount = 0; $recordCount < count($keys); $recordCount++){ $column = $keys[$recordCount]; echo '<td class="py-2">' . $row[$column] . '</td>'; } echo '</tr>'; } echo '</tbody> </table'; for($pages = 1; $pages <= $pageNumbers; $pages++){ echo '<a class="btn btn-dark mx-1" href=?page=' . $pages . '</a>'; } } ?> Any help/advice would be really appreciated I'm trying to develop a Joomla plugin for 1.6/1.7 and from what I've read, future versions are supposed to be php E~STRICT compliant. I know that doesn't mean I have to fix this since it is just a notice, but I am curious how one would hide the error. Code: [Select] $task_array = explode('.', JRequest::getVar('task')); # Splits article.save $task = $task_array[1]; #This is line 58 I know it happens because $task_array is empty, but how can i wrap that in something to stop this Notice error? I've tried !empty() but that doesn't work. Hi, I am new to the forums. I am having an issue with a snippet of code that generates random strings. I have already searched the forums, but the fix that I found is already in my code. I am still receiving the error though and was wondering if there was something else going on that I didn't see. Here is my code: Code: [Select] $Data = 'Ahmet'.md5(mt_rand(0, 123456789)); $length = 30; $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $string = ''; $string2 = ''; for ($a = 0; $a < $length; $a++) { $pos = mt_rand(0, strlen($chars)-1); $string .= $chars{$pos}; $pos2 = mt_rand(0, (strlen($Data))-1); $string2 .= $chars{$pos2}; } I appreciate any help on this, as it is cluttering my system log table. Thanks. Ok, I am sure you have all come across this error before. I am trying to understand why this occurs in a straight forward (spelled out for the complete dumb ass) kind of way. I made the most simple script I could come up with that would produce the error, but I am still uncertain as to what causes it, or what the fix is. Here is the script. Using all the letters and numbers available I print the total characters in the string, run a for statement to print out each character in order from the string index, and get the error "Undefined String Offset 36". Which is the total number of characters in the string starting at 0. The test script can be accessed at the following url: http://icca.exiled-alliance.com/classtest.php $test = "abcdefghijklmnopqrstuvwxyz1234567890"; $b = "<br />"; echo $test; echo $b; echo "There are " .strlen($test). " characters in the above string"; echo $b; for($x=0;$x<=strlen($test);$x++){ echo $test[$x]. "-->number $x"; echo $b; } Can anyone clear up why that error pops up from the way I have this scripted. I am writing a script that downloads stock data and graphs the prices using libchart charting library. After downloading data to array $prices, I use the following line to add a new data point to my chart:
$serie1->addPoint(new Point($prices[$i]['date'], $prices[$i]['price']);
$prices contains both date and price keys. However, this throws off illegal string offset error for both keys. I have checked with isset, both variables contain values. I have also tried typecasting, date with (string) and price with (float), but these threw off new errors.
What could be causing the illegal string offset error? Is there a way to silence it?
Edited by codingcasually, 01 November 2014 - 03:55 AM. Hi guys, why am i getting this error: Illegal string offset 'user_id' but when echo $value it brings the correct output. Thanks
$user_id = 5; $user_name = "obodo"; $_SESSION['test'] = array('user_id' => $user_id, 'user_name' => $user_name); foreach( $_SESSION['test'] as $value ) { echo $value['user_id']; //give error /* echo $value //works */ }
In PHP Version 8.0 shows error as : In previous versions of PHP it does not show any error. Please resolve the issue. Could someone here explain me the reason for this error in detail..... FYI - i am just more than a fresher in PHP..... Code: [Select] Fatal error: Cannot use string offset as an array okay this sounds strange I know but but apart from html code in pure php the database values display correctly but when I combine with HTML code i get an offset error when retrieving certain values example results from database file no HTML coding: February10 2012 5pm MY EVENT NEW YORK ERROR when combined with HTML: February MYEVENT 2012 NewYork I get an undefined offset error when i try and call the array i made i know very little so I'm stuck and i don't know what to do Code: [Select] <?php $file = fopen("playerstats.csv","r"); $data = array(); while (($csv_line = fgetcsv($file, 0, ";")) !== FALSE) { echo $csv_line; $number = $csv_line[0]; $name = $csv_line[1]; $half = $csv_line[2]; $goals = $csv_line[3]; $assists = $csv_line[4]; $SOG = $csv_line[5]; $shots = $csv_line[6]; $fouls = $csv_line[7]; $yellow = $csv_line[8]; $yellowred = $csv_line[9]; $red = $csv_line[10]; $CK = $csv_line[11]; $saves = $csv_line[12]; $data[] = array( 'name' => $name, 'half' => $half, 'goals' => $goals, 'assists' => $assists, 'sog' => $SOG, 'shots' => $shots, 'fouls' => $fouls, 'yellow' => $yellow, 'yellowred' => $yellowred, 'red' => $red, 'ck' => $CK, 'saves' => $saves ); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <table><tr><td>Number: </td><td><?php echo $data[0];?></td></tr></table> </body> </html> Hi everyone... please help me with this. I have a gallery that I am working on. Part of that are two files upload.php and preupload.php which upload pics. And it does its job successfully BUT it shows an error 'Notice: Undefined offset: 9 in C:\wamp\www\upload.php on line 34' My line 34 is Code: [Select] if($photos_uploaded['size'][$counter] > 0). My whole code for the upload.php is <?php include("config.inc.php"); // initialization $result_final = ""; $counter = 0; // List of our known photo types $known_photo_types = array( 'image/pjpeg' => 'jpg', 'image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/bmp' => 'bmp', 'image/x-png' => 'png' ); // GD Function List $gd_function_suffix = array( 'image/pjpeg' => 'JPEG', 'image/jpeg' => 'JPEG', 'image/gif' => 'GIF', 'image/bmp' => 'WBMP', 'image/x-png' => 'PNG' ); // Fetch the photo array sent by preupload.php $photos_uploaded = $_FILES['photo_filename']; // Fetch the photo caption array $photo_caption = $_POST['photo_caption']; while( $counter <= count($_FILES['photo_filename']['tmp_name']) ) { if($photos_uploaded['size'][$counter] > 0) { if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types)) { $result_final .= "File ".($counter+1)." is not a photo<br />"; } else { mysql_query( "INSERT INTO gallery_photos( `photo_filename`, `photo_caption`, `photo_category` ) VALUES( '0', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" ) or die(mysql_error() . 'Photo not uploaded'); $new_id = mysql_insert_id(); $filetype = $photos_uploaded['type'][$counter]; $extention = $known_photo_types[$filetype]; $filename = $new_id.".".$extention; mysql_query( "UPDATE gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" ); // Store the orignal file copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename); // Let's get the Thumbnail size $size = GetImageSize( $images_dir."/".$filename ); if($size[0] > $size[1]) { $thumbnail_width = 200; $thumbnail_height = (int)(200 * $size[1] / $size[0]); } else { $thumbnail_width = (int)(200 * $size[0] / $size[1]); $thumbnail_height = 200; } // Build Thumbnail with GD 1.x.x, you can use the other described methods too $function_suffix = $gd_function_suffix[$filetype]; $function_to_read = "ImageCreateFrom".$function_suffix; $function_to_write = "Image".$function_suffix; // Read the source file $source_handle = $function_to_read ( $images_dir."/".$filename ); if($source_handle) { // Let's create an blank image for the thumbnail $destination_handle = ImageCreateTrueColor ( $thumbnail_width, $thumbnail_height ); // Now we resize it ImageCopyResized( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] ); } // Let's save the thumbnail $function_to_write( $destination_handle, $images_dir."/tb_".$filename, 99 ); ImageDestroy($destination_handle ); // $result_final .= "<img src='".$images_dir. "/tb_".$filename."' /> File ".($counter+1)." Added<br />"; } } $counter++; } // Print Result echo <<<__HTML_END <html> <head> <title>Photos uploaded</title> </head> <body> $result_final </body> </html> __HTML_END; ?> First off, what are the names for the types of arrays? What do you call an array that uses integers for the key? I think when the key is text it is called an associative array? Anyways...
Is there a way to create an array where they keys are integers, but you are defining the first entry? I would like my array to either start with a "1" or maybe "1001". How do I do that?
im getting Undefined offset: 0 in many line what can be the issue? Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><!-- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> --> <title>Untitled Document</title> </head> <body> <?php if(isset($_POST['submit'])) { $userquery = $_POST['userquery']; $xml = simplexml_load_file("http://www.google.com/ig/api?weather='".$userquery."'"); $information = $xml->xpath("/xml_api_reply/weather/forecast_information"); $current = $xml->xpath("/xml_api_reply/weather/current_conditions"); $forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions"); echo "<h1>".$information[0]->city['data'].", ".$information[0]->forecast_date['data']."</h1>"; echo "<h2>Current Contition</h2>"; echo "<div class=\"weather\">"; echo "<table width='250' border='1'>"; echo "<tr>"; echo "<td><img src='http://www.google.com".$current[0]->icon['data']."' width='70' height='70' alt='weather'></td>"; echo "<td>"; echo $current[0]->condition['data']; echo "<br/>Min "; echo $current[0]->temp_c['data']."° C, <br/>"; echo $current[0]->temp_f['data']."° F, <br/>"; echo $current[0]->humidity['data'].", <br/>"; echo $current[0]->wind_condition['data']."."; echo "</td>"; echo "</tr>"; echo "</table>"; echo "</span>"; echo "</div>"; echo "<h2>Forecast</h2>"; foreach ($forecast_list as $forecast) : echo "<div class=\"weather\">"; echo "<table width='250' border='1'>"; echo "<tr>"; echo "<td>"; echo "<img src='http://www.google.com".$forecast->icon['data']."' width='50' height='50'alt='weather'>"; echo "</td>"; echo "<td>"; echo "<div>".$forecast->day_of_week['data']."</div>"; echo "<span class=\"condition\">"; echo $forecast->low['data']."° F - ".$forecast->high['data']."° F,<br/>"; echo $forecast->condition['data']."."; echo "</td>"; echo "</tr>"; echo "</table>"; echo "</span>"; echo "</div>"; endforeach; } ?> <form action="test.php" method="post"> <input name="userquery" type="text" value=""/> <input type="submit" name='submit' value="get"/> </form> </body> </html> here all errors and there are few more Notice: Undefined offset: 0 in C:\wamp\www\fiunc\test.php on line 16 Notice: Trying to get property of non-object in C:\wamp\www\fiunc\test.php on line 16 Notice: Undefined offset: 0 in C:\wamp\www\fiunc\test.php on line 16 Notice: Trying to get property of non-object in C:\wamp\www\fiunc\test.php on line 16 , it happens when i add if(isset($_POST['submit'])) { $userquery = $_POST['userquery']; and this in '".$userquery."' $xml = simplexml_load_file("http://www.google.com/ig/api?weather='".$userquery."'"); What I am trying to do is to associate a user (id) to a page they create, so if joe blogs is user 4, then the data base will show the page details, and add the id (from a user page). However, when I try the code to save data I get the following error messages:
Notice: Undefined offset: 4 in C:\xampp\htdocs\MyCMS\admin\index.php on line 98 I include the code to insert the page: <!-- Insert Page --> <?php if(isset($_POST['submitted']) == 1) { $header = stripslashes($_REQUEST['header']); $header = mysqli_real_escape_string($dbc, $header); $title = stripslashes($_REQUEST['title']); $title = mysqli_real_escape_string($dbc, $title); $body = stripslashes($_REQUEST['body']); $body = mysqli_real_escape_string($dbc, $body); $userid = stripslashes($_REQUEST['userid']); $userid = mysqli_real_escape_string($dbc, $userid); $q = "INSERT INTO `pages` (`userid`, `header`, `title`, `body`) VALUES ($_POST[$userid], '$header', '$title', '$body')"; $r = mysqli_query($dbc, $q) or die(mysqli_error($dbc)); if($r) { $message = '<p>Page was added.</p>'; } else { $message = '<p>Page could not be added due to: </p>'.mysqli_error($dbc); $message .= '<p>'.$q.'</p>'; } // end if inner } // end if outer ?> The form has - Page Header, Page Title, User, and boys. There is a working list of current users. It is these users (and new users) that I want to add to a 'Pages' database, which has the following fields: id, userid, header, title, body; and a 'User' database with the following fields: id, firstname, lastname, username, password, status. Any help to solve the issues will be appreciated. Thanks.
Using the following code: $qwe = array('a','b','c','d','e'); for($i=0; $i<5; $i++) { $asd = rand(0, sizeof($qwe)-1); echo $qwe[$asd]; unset($qwe[$asd]); } gives the following error: Notice: Undefined offset: <offset> in <directory>\<file>.php on line 7 |