Jump to content

ale1981

Members
  • Posts

    65
  • Joined

  • Last visited

Posts posted by ale1981

  1.  

    Your way words just fine. The other way would be to use regex

    field = field.replace(/(\w+)\.(\d+)\.(\w+)/g, '$1[$2][$3]');
    

     

    Thanks, I knew there would be a regex way, I am just useless when it comes to regex!
     
    I am guessing the .replace is quicker as it is done with one function?
  2. Your question is not clear are you wanting convert the string 'products.0.name' to the string 'products[0][name]' or are wanting to convert it to array notation so you can return the name item from the array at index 0 of the variable named products?

     

    sorry, yes, i would like the string 'products.0.name' converted to string 'products[0][name]'

  3. I have a string e.g.

     

    products.0.name

     

    and I want to change the string to;

     

    products[0][name]

     

    I have come up with some code that works but is a bit of a hack, my code is below;

    var array = field.split(".");
    field = array[0]+'['+array[1]+']['+array[2]+']';
    
    

    Is there a better way, I am sure there is using regex?

     

     

    Thanks

  4. There is always a different way, the question is if there is a better way or not - and in this case there is. I was actually going to speak to the same problem that I'm sure benanamen was going to address, but decided against it once Ch0cu3r responded.

     

    You should not have data where there is an assumption of correlation based on position. This is bad form and will eventually lead to bugs. Instead, the correlated data should be structured such that the correlation is specifically defined. In this case you have pairs of fields that should be correlated within the structure. Using a multidimensional array was the right choice - but I would swap the keys so that the records are logically structured. But, instead of "[]" you will need to define the key.

     

    E.g.

    <input type="text" name="productVariations[0][name] />
    <input type="text" name="productVariations[0][price]" />
     
    <input type="text" name="productVariations[1][name] />
    <input type="text" name="productVariations[1][price]" />
     
    <input type="text" name="productVariations[2][name] />
    <input type="text" name="productVariations[2][price]" />
    

    I'm sure I've left off some content that goes between the fields and, ideally, the output would be generated within a loop. using that structure, the array of the post data would look like this

    productVariations array (
      0 => ("name" => "Default", "price" => "12.99"),
      1 => ("name" => "100ml", "price" => "9.99"),
      2 => ("name" => "150ml", "price" => "14.99")
    )

     

    Then the code to process the data could look like this

    foreach($productVariationsAry as $record)
    {
        $name = $record['name'];
        $price = $record['price'];
     
       // do insert query
    }

    FYI: It would also be more efficient to create ONE insert query to insert all records at one.

     

    Thanks for the detailed response.

     

    I am adding the fields dynamically so I do not know how many the user is going to have, I guess I could use a bit of JS to determine this.

     

    Yes the SQL query will be one query, I used the above as an example of what I needed.

     

     

    Thanks for all responses.

  5. Use a foreach loop to iterative over the names array, using the array key to get the corresponding value in the prices array

    // loop over the names array
    foreach($array['names'] as $key => $name)
    {
        // use the array key to reference the corresponding value in the prices array
        $price = $array['prices'][$key];
    
       // do insert query
    }
    

    Change $array to be your variable that contain the names and prices array

     

    Thanks Ch0cu3r that worked, I knew there would be a simple answer!

     

    I would like to see your form.

     

    I have 2 input fields that use the same variable array, e.g.

    <input type="text" name="productVariations[name][]" />
    <input type="text" name="productVariations[price][]" />
    
    

    Is there a different way?

  6. It's late in the day and I can't figure this one out, I know it's going to be pretty easy!

     

    I have a form which sends the following array;

     

    array:2 [

    "names" => array:3 [

    0 => "Default"

    1 => "100ml"

    2 => "150ml;"

    ]

    "prices" => array:3 [

    0 => "12.99"

    1 => "9.99"

    2 => "14.99"

    ]

     

    I want to loop the array and do an insert using this array like so, the index from "names" and "prices";

    INSERT INTO DB VALUES('Default',12.99);
    INSERT INTO DB VALUES('100ml',9.99);
    INSERT INTO DB VALUES('150ml',14.99);
    

    Thanks

  7. Hi BlueSky

     

    I have since amended the files, i have removed session_id() and the second instance of session_start() in the function.

    I used the session_save_path to make sure the session files were being created.

     

    With more testing, if I call the function get_user_details() every time in the header.php file then $_POST works fine, however if i only call it once to store the user details then $_POST doesnt work!? Is it something inside the function that is authenticating?

  8. New update!

     

    I think it is something in the function affecting $_POST.

     

    I have removed the function call and tried to create a session and everything seems to work, what in the function could be affecting the $_POST details, could it be the header calls?

  9. PHP 5.2.6

    Apache 2.2.9

     

    I am trying to store windows username / domain / workstation details into a session for our company intranet. I found a script on the internet that I can use to retrieve the information which I made into a function;

     

    function get_user_details() {
    
    // loune 25/3/2006, updated 22/08/2009
    // For more information see:
    // http://siphon9.net/loune/2007/10/simple-lightweight-ntlm-in-php/
    // 
    // This script is obsolete, you should see
    // http://siphon9.net/loune/2009/09/ntlm-authentication-in-php-now-with-ntlmv2-hash-checking/
    //
    
    // NTLM specs http://davenport.sourceforge.net/ntlm.html
    
    $headers = apache_request_headers();
    
    if (!isset($headers['Authorization'])){
    	header('HTTP/1.1 401 Unauthorized');
    	header('WWW-Authenticate: NTLM');
    	exit;
    }
    
    $auth = $headers['Authorization'];
    
    if (substr($auth,0,5) == 'NTLM ') {
    	$msg = base64_decode(substr($auth, 5));
    	if (substr($msg, 0,  != "NTLMSSP\x00")
    		die('error header not recognised');
    
    	if ($msg[8] == "\x01") {
    		$msg2 = "NTLMSSP\x00\x02\x00\x00\x00".
    			"\x00\x00\x00\x00". // target name len/alloc
    			"\x00\x00\x00\x00". // target name offset
    			"\x01\x02\x81\x00". // flags
    			"\x00\x00\x00\x00\x00\x00\x00\x00". // challenge
    			"\x00\x00\x00\x00\x00\x00\x00\x00". // context
    			"\x00\x00\x00\x00\x00\x00\x00\x00"; // target info len/alloc/offset
    
    		header('HTTP/1.1 401 Unauthorized');
    		header('WWW-Authenticate: NTLM '.trim(base64_encode($msg2)));
    		exit;
    	}
    	else if ($msg[8] == "\x03") {
    		function get_msg_str($msg, $start, $unicode = true) {
    			$len = (ord($msg[$start+1]) * 256) + ord($msg[$start]);
    			$off = (ord($msg[$start+5]) * 256) + ord($msg[$start+4]);
    			if ($unicode)
    				return str_replace("\0", '', substr($msg, $off, $len));
    			else
    				return substr($msg, $off, $len);
    		}
    		$user = get_msg_str($msg, 36);
    		$domain = get_msg_str($msg, 28);
    		$workstation = get_msg_str($msg, 44);
    
                           	session_start();
    		$_SESSION['username'] = $user;
    		$_SESSION['domain'] = $domain;
    		$_SESSION['workstation'] = $workstation;
    	}
    }
    }
    

     

    What i would like to do is store the information in a session so I can access the information without having to call the function on every page. I have a header.php file which is called on each page, so in this file i have added;

     

    require_once 'functions.php';
    session_id();
    session_start();
    if (!$_SESSION['username']) {
        get_user_details();
    }
    

     

    This works, but seems to be breaking certain pages that retrieve $_POST details but I can not figure out why??

     

    I have tried to work it out for the past 3 days but cant seem to get it to work, any help would be greatly appreciated!  :shrug:

  10. SOLUTION for anybody interested;

     

    <table>
    <?php
    while ($ord_inf = mssql_fetch_array($result))
    {
            if($lastOrderNo != $ord_inf['ORDERNO'] || $lastOrderNo == "")
            {
                    $trclass = ($trclass == 'alt2') ? 'alt1' : 'alt2';
            }
            $lastOrderNo = $ord_inf['ORDERNO'];
            ?>
            <tr class="<?php echo $trclass; ?>">
              <td><?php echo $ord_inf['ORDERNO']; ?></td>
            </tr>
            <?php
    }
    ?>
    </table>
    

  11. I can not figure this out, what I want to do is alternate the row colour of a table depending on a variable when looping through results from a db query, example;

     

    ORD123

    ORD123

    ORD124

     

    and so on..

     

    <table>
    $trclass = 'alt1'
    $trclass = 'alt2'
    while ($ord_inf = mssql_fetch_array($result)) {
      if ($ordinf['ORDERNO'] == $orderNo) {
        'keep same tr class as its the same order'
      } else {
        'its a different order so change the tr class'
      }
        <tr class="$trclass">
          <td>$ord_inf['ORDERNO']</td>
        </tr>
      $orderNo = $ord_inf['ORDERNO'];
    }
    </table>
    

     

    something like that, i just cant seem to get it to work!

     

    any help appreciated...

     

    [/]

  12. If I have an array similar to;

     

    Array ( [0] => 0010 [1] => 0020 [2] => 0030 [3] => 0040 [4] => 0050 [5] => 0060 )
    

     

    How can i convert that array to a string, so the string will be like;

     

    "0010", "0020", "0030", "0040", "0050", "0060"
    

     

    Notice the "," is missing from the last part of the array.

    If the array has only one entry then it would be;

     

    "0010"
    

     

    Any help appreciated.

     

    Thanks in advance.

  13. Is it possible for me to combine these 2 SQL queries so I just get a combined Sales figure from both tables?

     

    
    SELECT     SUM(SUBTOTAL) AS Sales
    FROM         SOP30200
    WHERE     (CUSTNMBR = 'HLS0003') AND (SOPTYPE = 2) AND (PRSTADCD = '0012')
    
    SELECT     SUM(SUBTOTAL) AS Sales
    FROM         SOP10100
    WHERE     (CUSTNMBR = 'HLS0003') AND (SOPTYPE = 2) AND (PRSTADCD = '0012')
    
    

     

    Thanks in advance.

  14. i would define an array of usernames you don't want to log in prior to any of the if statments and then just add another else at the end of your script like this

     

    
    $disallow = array("username1","username2","username3");
    
    //then add something like this within your if/else
    
    else if(in_array($disallow)){
      //the code that generates the error message or whatever
    }
    

     

    It's probably not the most robust way, but it should get the job done.

     

    http://us2.php.net/manual/en/function.in-array.php //the link to the function at php.net

     

     

     

    I would use the solution above or by storing the disallowed names in lowercase form in a db table and query the table;

     

    
    strtolower($_POST['username']);
    $sql = 'SELECT * FROM dissallowed_users WHERE username="' .$_POST['username']. '"';
    
    

  15. That still repeats, like;

     

    Driver Manifest Date 
    The following 1 PDF(s) have been found: 
    PW 22/10/2007 View 
    The following 1 PDF(s) have been found: 
    PW 19/10/2007 View 
    

     

    I would like it to just show;

     

    The following 2 PDF(s) have been found:
    
    Driver Manifest Date 
    PW 22/10/2007 View 
    PW 19/10/2007 View
    

     

     

  16. I have been trying to work this out all afternoon and there is probably an easy solution, but I can not think of it!

     

    My current code loops through a result of order number's. Now if there is just one manifest returned for that order number, this is fine, BUT, if there is more than one i obviously get the 2 tables instead of just the one with 2 rows. I know its because the while is looping through each order number and therefore showing the code below for each manifest. How can i re-arrange this so that just one table is shown ?

     

    	while($man_inf = mssql_fetch_array($result)) {
    		$man_date =	convert_date($man_inf['PDATE']);
    		$sql2 = 'SELECT pid, title, driver, man_date, size FROM manifestPDF WHERE (man_date = ' .$man_date. ') AND (driver = "' .trim($man_inf['UPSZONE']). '")';
    		$result2 = mysql_query($sql2);
    		$num_PDFs = mysql_num_rows($result2);
    		echo '<p>The following ' .$num_PDFs. ' PDF(s) have been found: </p>';
    ?>
    		<table class="sep" width="250">
    		<tr>
    			<td class="title">Driver</td>
    			<td class="title" colspan="2">Manifest Date</td>
    		</tr>
    <?
    		if ($num_PDFs > 0) {
    			while($PDF_inf = mysql_fetch_array($result2)) {
    				$man_date = date('d/m/Y', $PDF_inf['man_date']);
    ?>
    				<tr>
    					<td><?=$PDF_inf['driver'];?></td>
    					<td><?=$man_date?></td>
    					<td><a href="view_manifest.php?id=<?=$PDF_inf['pid']?>">View</a></td>
    				</tr>
    <?
    			}
    		echo '</table>
    		<p><a href="javascript: history.go(-1)">SEARCH AGAIN</a></p>';
    		} else {
    			echo '<p>No Manifests found with that criteria.</p>
    			<p><a href="javascript: history.go(-1)">SEARCH AGAIN</a></p>';
    		}
    	}
    

     

    Thanks in advance and apologise if its a simple solution, my brain is dead today.

     

  17. Hi guys,

     

    I am a bit stuck! Here is what I need to do;

     

    User scans a document in and sends the document to a mailbox. I then need a script to log into that mailbox every x minutes to check to see if there is any new emails. If there is I need to download the attachment to a location and then once the file has been saved succesfully, i need to mark it so it wont get read again.

     

    The purpose of this is so that I can then display these PDFs on an intranet page so that users can view the documents, i would prefer it to be all automated instead of me going through the inbox manually and moving them myself.

     

    I have had experience in sending emails with attachments, but never reading them and saving the attachments.

     

    Any help would be very much appreciated.

     

     

    Thanks.

×
×
  • 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.