Jump to content

ruraldev

Members
  • Posts

    41
  • Joined

  • Last visited

Posts posted by ruraldev

  1. I have the following code which works (thanks to PHP Freaks members)

        foreach ($swap["sims"] as $swap) 
        		{
                        echo $swap['voice'] . "</br>";
    			}
    
    

    But I can't figure out why the following doesn't work

    <select name="sim_for_swap[]">
    <?php
        foreach ($swap["sims"] as $swap)  
        		{
    ?>
    <option value="<?php echo $swap['voice'];?>"><?php echo $swap['voice'];?></option>
    <?php
    }
    ?>
    
  2. Thank you for not only taking the time to give me the correct code but also to provide some workings which I can use to understand the process.

     

    It returns the values I want but for some reason I get an error at the bottom of the page: Warning: Invalid argument supplied for foreach() in .......

     

    For line:   foreach ($stock["sims"] as $sim)

     

    Any ideas?

        foreach ($stock["stock"] as $stock) 
        		{
        			foreach ($stock["sims"] as $sim)
        				{
                        echo $sim . "</br>";
                		}
    			}
    
    
  3. I am trying in vain to access the numbers in the "sims" array using the code below, I have tried various numbers of nested foreach loops but get errors every time, can anyone help point me in the correct direction please.

     

    Thanks

     

    Gordon

    $stock = (array(1) 
    	{ ["stock"]=> array(2) 
    		{ [0]=> array(2) 
    			{ ["operator"]=> string(3) "ECL" 
    				["sims"]=> array(51) 
    					{ 
    					[0]=> int(8944122650438410000) 
    					[1]=> int(8944122650438409000) 
    					[2]=> int(8944122650438409000)
    		 			} 
    		 	} 
    		[1]=> array(2) { 
    			["operator"]=> string(2) "JT" 
    				["sims"]=> NULL 
    } } } )
    
    
    foreach ($stock as $key1 => $item1)
                { 
                    foreach($item1 as $key2 => $item2)
                    { 
                    		foreach($item2 as $key => $value)
                    		{
                        		echo $value[sims] . "</br>";
                    		}
                	}
    			}
    
  4. Thanks for the help, I suppose that's what happens when I take bits of code from various samples!

     

    I have run the query manually to make sure it returns the 8 rows of data and it does work.

     

    I'll try outputting text instead of sending emails to see what happens.

     

    Cheers

     

    Gordon

  5. Can anyone help me with why this code would only send 1 email when the query returns 8 rows?

     

    I am pretty sure it will be simple but I can't find the problem!

    $sql = "SELECT email, supplier_id FROM tbl_suppliers";
    $stmt = $db->prepare($sql);
    $stmt->execute();
    
    foreach($db->query($sql) as $row){
    	
    	$supp_id = $row['supplier_id'];
    	
    	$emailCSV->setEmailMessage("some generic text message");
    
    	$email = $row['email'];     
    	$emailCSV->sendEmail("me@me.com",$email,"Quote Request");
    }
    
  6. Managed to get it almost perfect, code below in case it helps anyone else.

     

    $ras = stream_socket_client($svr_add,$errno,$errstr,$svr_timeout);
    
    fputs ($ras, $xml_data);
    
    stream_set_timeout($ras, 1);
    
    $ras1 = stream_get_contents($ras, -1);

     

    It takes 1 second but that's not a problem, I'll probably play with it to get the perfect timeout value.

     

    Thanks for the help

     

    Gordon

  7. I am using stream_get_contents and if I specify a length which is less than the stream length it returns the page quickly, if I use -1 or a value higher than the stream length it takes more than a minute!

     

    Very slow versions

    stream_get_contents($ras, 8000, -1);
    stream_get_contents($ras, -1, -1);

     

    Very quick

    stream_get_contents($ras, 7500, -1);

     

    The stream length varies so I don't have any idea what value will speed it up!

     

    Any solutions?

  8. I am opening a connection to a server to allow me to send a xml command which returns xml data to me, however when the connection is made I receive data from the server, once I send the xml command the returned xml data is appended to the original data from the server.

     

    My question is can I clear the file of the connection data or ignore it totally or over-write it?

     

    The code all works, it is just this extra data I want to remove.

     

    This is my current code:

     

    $xml_data ="<xml string to be sent to server goes here>";
    $ras = fsockopen($svr_add,$svr_port,$errno,$errstr,$svr_timeout);
    
    fputs ($ras, $xml_data);
    
    $ras1 = stream_get_contents($ras, -1, -1);
    
    fclose($ras);
    
    $xml = $ras1;
    
    echo print_r($xml);

  9. Code is below, however it wouldn't work live as the remote machine is behind a firewall and only certain IP's are allowed through. It does get a response as I can see it but it is just a single long string.

     

    You may well be correct that this is beyond my abilities!

     

     

    $var = fsockopen($svr_add,$svr_port,$errno,$errstr,$svr_timeout);

     

    if(!$var)

        {

        echo "Connextion Failed\n";

        exit();

        }

    else

        {

    fputs ($var, $xml_data);

     

    echo fgets ($var, 128);

     

    fclose($var);

        }

     

  10. Can anyone help me print the output from fsockopen, I create a variable called $var

     

    It returns an xml format

     

    <tag>

        <element1>one</element1>

        <element2>two</element2>

        <element3>three</element3>

    </tag>

     

    I can output the values onetwothree using fputs and fgets but I need to be able to display just a single element, I am sure this is simple but I still can't work it out.

     

    Hope someone can help.

     

    Thanks

     

    Gordon

  11. Apologies if this should be in the mysql forum but hopefully this is the correct one.

     

    I am trying to extract the sum of a column from my database for each financial year, at the moment I am doing each year as a separate recordset but I am certain there must be a more automated way!

     

    The financial year is 1st April to 31st March each year and I need to create a variable which is a sum of each year, you'll see in my code below what I mean, any help gratefully appreciated as I am going to end up with decades of info in the db and am keen to get the code right now!

     

    The relevant recordsets are 2,5,7.  db is readingID, date, reading

     

    The code and page does work fine, it's just long winded!

     

    <?php
    mysql_select_db($database_wind, $wind);
    $query_Recordset1 = "SELECT readingID, date_format(date,'%d/%m/%Y') as date, reading FROM solar ORDER BY readingID DESC LIMIT 5";
    $Recordset1 = mysql_query($query_Recordset1, $wind) or die(mysql_error());
    $row_Recordset1 = mysql_fetch_assoc($Recordset1);
    $totalRows_Recordset1 = mysql_num_rows($Recordset1);
    
    mysql_select_db($database_wind, $wind);
    $query_Recordset2 = "SELECT SUM(reading) as year1total FROM `solar` WHERE date between '2010-04-01' and '2011-03-31'";
    $Recordset2 = mysql_query($query_Recordset2, $wind) or die(mysql_error());
    $row_Recordset2 = mysql_fetch_assoc($Recordset2);
    $totalRows_Recordset2 = mysql_num_rows($Recordset2);
    
    mysql_select_db($database_wind, $wind);
    $query_Recordset3 = "SELECT * FROM solar ORDER BY readingID DESC Limit 29";
    $Recordset3 = mysql_query($query_Recordset3) or die(mysql_error());
    
    mysql_select_db($database_wind, $wind);
    $query_Recordset4 = "SELECT readingID, YEAR(date) as yeardate, MONTHNAME(date) as monthdate, SUM(reading) as sumreading FROM `solar` Group by yeardate, monthdate Order by readingID ASC";
    $Recordset4 = mysql_query($query_Recordset4) or die(mysql_error());
    
    mysql_select_db($database_wind, $wind);
    $query_Recordset5 = "SELECT SUM(reading) as year2total FROM `solar` WHERE date between '2011-04-01' and '2012-03-31'";
    $Recordset5 = mysql_query($query_Recordset5, $wind) or die(mysql_error());
    $row_Recordset5 = mysql_fetch_assoc($Recordset5);
    $totalRows_Recordset5 = mysql_num_rows($Recordset5);
    
    mysql_select_db($database_wind, $wind);
    $query_Recordset6 = "SELECT datediff( Max(date), Min(date)) as DateDiff, Sum(reading) as LatestReading, date_format(MAX(date),'%d/%m/%Y') as LatestDate FROM solar";
    $Recordset6 = mysql_query($query_Recordset6, $wind) or die(mysql_error());
    $row_Recordset6 = mysql_fetch_assoc($Recordset6);
    $totalRows_Recordset6 = mysql_num_rows($Recordset6);
    
    mysql_select_db($database_wind, $wind);
    $query_Recordset7 = "SELECT SUM(reading) as year3total FROM `solar` WHERE date between '2012-04-01' and '2013-03-31'";
    $Recordset7 = mysql_query($query_Recordset7, $wind) or die(mysql_error());
    $row_Recordset7 = mysql_fetch_assoc($Recordset7);
    $totalRows_Recordset7 = mysql_num_rows($Recordset7);
    
    $average = (int)(($row_Recordset6['LatestReading'])/($row_Recordset6['DateDiff']));
    
    $income1 = $row_Recordset2['year1total']*0.428;
    $income2 = $income1 + $row_Recordset5['year2total']*0.464;
    $income = $income2 + $row_Recordset7['year3total']*0.464;
    
    $saving = $row_Recordset2['year1total']*0.0675;
    $saving = $saving + $row_Recordset5['year2total']*0.0675;
    $saving = $saving + $row_Recordset7['year3total']*0.0675;
    
    ?>

  12. Hi

     

    I am trying to tidy up a bit of php code to make the end display much better but I am a bit stuck, I have the following code which takes a mysql query and uses the result to create a google graph.

     

    <?php
    $str = "";
      $str .=  "<img src='http://chart.apis.google.com/chart?chs=600x300";
      $str .= "&chd=t:"; 
    
    
    $i=0;
    while($row_Recordset3 = mysql_fetch_assoc($Recordset3)){
    $reading_array[$i] = $row_Recordset3['reading'];
    $date_array[$i] = date("j/n", strtotime($row_Recordset3['date']));
    $i ++;
    }
    $count=0;
    $count = count($reading_array);
    if($count>1) {
    $rstring = implode(",", (array_reverse($reading_array)));
    $dstring = implode("|", (array_reverse($date_array)));
    }else{
    echo $count;
    }
    
    
    $str = $str.$rstring;
    
      $str = $str."&cht=lc&chxl=0:|";
      // We will close the src attribute with \' and to print escape character ' we shall precede it with \
    
    $str = $str.$dstring;
    
      $str = $str."&chxt=x,y&chds=0,25&chxr=1,0,25,5&chm=o,0044FF,0,-1,8|b,3399CC44,0,1,0&chtt=Electricity Generation for the last 35 days'";
      $str = $str." >";
    
      //we shall echo the $str that will display the graph
      ?>
      <tr><td>
      <?php
      echo $str;
    ?>
    

     

    Currently the result is

    http://chart.apis.google.com/chart?chs=600x300&chd=t:10,6,1,11,12,7,6,2,1,1,4,10,1,5,12,3,3,6,1,6,3,3,1,5,3,1,1,6,8,5,8,8&cht=lc&chxl=0:|27/10|28/10|29/10|30/10|31/10|1/11|2/11|3/11|4/11|5/11|6/11|7/11|8/11|9/11|10/11|11/11|12/11|13/11|14/11|15/11|16/11|17/11|18/11|19/11|20/11|21/11|22/11|23/11|24/11|25/11|26/11|27/11&chxt=x,y&chds=0,25&chxr=1,0,25,5&chm=o,0044FF,0,-1,8|b,3399CC44,0,1,0&chtt=Electricity%20Generation%20for%20the%20last%2035%20days

     

    I need to make it display only every 7 dates so that they are readable but I still need the pipes in between to space out the dates correctly so they tie in with the correct point on the graph, like this where I manually removed some dates as a demo

    http://chart.apis.google.com/chart?chs=600x300&chd=t:10,6,1,11,12,7,6,2,1,1,4,10,1,5,12,3,3,6,1,6,3,3,1,5,3,1,1,6,8,5,8,8&cht=lc&chxl=0:|27/10|||||1/11|||||6/11|7/11|8/11|9/11|10/11|11/11|12/11|13/11|14/11|15/11|16/11|17/11|18/11|19/11|20/11|21/11|22/11|23/11|24/11|25/11|26/11|27/11&chxt=x,y&chds=0,25&chxr=1,0,25,5&chm=o,0044FF,0,-1,8|b,3399CC44,0,1,0&chtt=Electricity%20Generation%20for%20the%20last%2035%20days

     

    This is beyond my limited stills, can anyone help please?

     

    Thanks

     

    Gordon

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.