Jump to content

shamuraq

Members
  • Posts

    140
  • Joined

  • Last visited

Posts posted by shamuraq

  1. 14 minutes ago, Barand said:

    You must be using an outdated version of php. Str_starts_with() requires version 8.

    Use this instead

    $required = array_filter($files, fn($v)=>substr($v, 0, 5) == 'alg00');

     

    What does the values in 0,5 in substr($v, 0, 5) refers to?

  2. 32 minutes ago, Barand said:

    Easier with str_starts_with()

    $files = [  'alg001.php',
                'alg002.php',
                'alg003.php',
                'alg004.php',
                'alg005.php',
                'algComp.php',
                'ranFrac.php',
                'ranalg.php',
                'randec.php',
                'randint.php'
              ];
    $required = array_filter($files, fn($v)=>str_starts_with($v, 'alg00'));
    echo '<pre>' . print_r($required, 1) . '</pre>';

    Gives

    Array
    (
        [0] => alg001.php
        [1] => alg002.php
        [2] => alg003.php
        [3] => alg004.php
        [4] => alg005.php
    )

     

    I tried as suggested:

    $arrFiles = array();
    $dirPath = "./";
    $files = scandir($dirPath);
    $required = array_filter($files, fn($v)=>str_starts_with($v, 'alg00'));
    echo '<pre>' . print_r($required, 1) . '</pre>';

    The error:

    Fatal error: Uncaught Error: Call to undefined function str_starts_with() ...php:29 Stack trace: #0 [internal function]: {closure}('.') #1 /...php(29): array_filter(Array, Object(Closure)) #2 {main} thrown in ...php on line 29

     

  3. Hi guysm

    I have a folder containing these files:

    alg001.php
    alg002.php
    alg003.php
    alg004.php
    alg005.php
    algComp.php
    ranFrac.php
    ranalg.php
    randec.php
    randint.php

    I would only like to store the names of files alg001.php, alg002.php, alg003.php, alg004.php, alg005.php into an array. How do i achieve this with preg match?

  4. 11 hours ago, Barand said:

    You've done the hard work already. Instead of calculating the product, store the selected array.

    <?php
    $primes = array(2, 3, 5, 7, 11, 13, 17, 19, 23);
    $combos = [];
    
    function getAllCombinations($arr, $n, &$combos, $selected = array(), $startIndex = 0) {
      if ($n == 0) {
          $combos[] = $selected;
    //    $product = 1;
    //    foreach ($selected as $prime) {
    //      $pr[] = $prime;
    //      $product *= $prime;
    //      $pr[] = $prime;
    //    }
    //    echo "Product: $product\n";
        return;
      }
    
      for ($i = $startIndex; $i < count($arr); $i++) {
        $selected[] = $arr[$i];
        getAllCombinations($arr, $n - 1, $combos, $selected, $i + 1);
        array_pop($selected); // Backtrack and remove the element for next iteration
      }
    }  
    
    getAllCombinations($primes, 4, $combos);
    
    echo '<pre>';
    foreach ($combos as $com)  {
        printf("%-35s = %5d<br>", join(' &times; ', $com), array_product($com));    // output numbers and product
    }
    
    ?>

    giving

    2 × 3 × 5 × 7     =   210
    2 × 3 × 5 × 11    =   330
    2 × 3 × 5 × 13    =   390
    2 × 3 × 5 × 17    =   510
    2 × 3 × 5 × 19    =   570
    2 × 3 × 5 × 23    =   690
    2 × 3 × 7 × 11    =   462
    2 × 3 × 7 × 13    =   546
    2 × 3 × 7 × 17    =   714
    2 × 3 × 7 × 19    =   798
    2 × 3 × 7 × 23    =   966
    2 × 3 × 11 × 13   =   858
    2 × 3 × 11 × 17   =  1122
    2 × 3 × 11 × 19   =  1254
    2 × 3 × 11 × 23   =  1518
    2 × 3 × 13 × 17   =  1326
    2 × 3 × 13 × 19   =  1482
    2 × 3 × 13 × 23   =  1794
    2 × 3 × 17 × 19   =  1938
    2 × 3 × 17 × 23   =  2346
    2 × 3 × 19 × 23   =  2622
    2 × 5 × 7 × 11    =   770
    2 × 5 × 7 × 13    =   910
    .
    .
    5 × 17 × 19 × 23  = 37145
    7 × 11 × 13 × 17  = 17017
    7 × 11 × 13 × 19  = 19019
    7 × 11 × 13 × 23  = 23023
    7 × 11 × 17 × 19  = 24871
    7 × 11 × 17 × 23  = 30107
    7 × 11 × 19 × 23  = 33649
    7 × 13 × 17 × 19  = 29393
    7 × 13 × 17 × 23  = 35581
    7 × 13 × 19 × 23  = 39767
    7 × 17 × 19 × 23  = 52003
    11 × 13 × 17 × 19 = 46189
    11 × 13 × 17 × 23 = 55913
    11 × 13 × 19 × 23 = 62491
    11 × 17 × 19 × 23 = 81719
    13 × 17 × 19 × 23 = 96577

     

    Tes... Thats exactly what i wanted. But can you explain the breakdown of "%-35 = %5d <br>"?

  5. I have a function randomly take 4 unique numbers from an array and multiply them. Problem with the script below is it only print out the products. I would like for each set of 4 numbers and its product to be in its own array and all the arrays into a main array, forming a 2 dimensional array. My current script:

    //Pruning outcome from controlled lists of Prime Numbers
    $primes = array(2, 3, 5, 7, 11, 13, 17, 19, 23);
    
    
    function getAllCombinations($arr, $n, $selected = array(), $startIndex = 0) {
      if ($n == 0) {
        $product = 1;
        foreach ($selected as $prime) {
          $pr[] = $prime;
          $product *= $prime;
          $pr[] = $prime;
        }
        echo "Product: $product\n";
        return;
      }
    
      for ($i = $startIndex; $i < count($arr); $i++) {
        $selected[] = $arr[$i];
        getAllCombinations($arr, $n - 1, $selected, $i + 1);
        array_pop($selected); // Backtrack and remove the element for next iteration
      }
    }  
    
    getAllCombinations($primes, 4);
    

    The output:

    Product: 210 Product: 330 Product: 390 Product: 510, etc.

    The preferred method is a 2 dimensional array as such:

    [0][0] =>2 [0][1] =>3 [0][2]=>5 [0][3]=>7 and the product would be in [0][4] =>210
    
    [1][0] =>2 [1][1] =>3 [1][2]=>5 [1][3]=>11 and the product would be in [1][4] =>330
    
    etc.

    Any pointers is greatly appreciated.

  6. Its like this:'

    $value = array(
           array(3075,15,461.25,16,535.05),
           array(3075,15,461.25808,16,535.05),//to be removed
           array(3075,15,461.2,16,535.05234)//to be removed
    );
    
    
    $value = array_filter($value,
       function ($v) {
           foreach ($v as $x) {
               if (strlen(substr(strrchr($x, "."), 1)) > 2)
                   return false;
           }
           return true;
       });
    
    
    var_dump($value);
    

     

    But nothing came out during testing. What could be wrong?

  7. Hi guys,

     

    am not very good with regex. I need to replace all double backslashes '\\' in my string into single backslashes '\'. I tried:

     

     

    $pattern = '[\\]';
    $replacement = '/\/';
    
    ?>
           <td width="100%"> <?php echo preg_replace($pattern, $replacement,$q[$i]);?></td>

     

    i also tried a few other combinations but the output is either blank or more backslashes are added. Any pointers are appreciated.

    Thanx in advance.

  8. Hi all,

    I'm trying to list the possible combinations in fraction from 1/1 + 1/1 until 9/9 + 9/9. So i came up with this

    <?php
    //numbers of combination for w/x + y/z
    $num1=1;
    $den1=1;
    $num2=1;
    $den2=1;
    $row=1;
    for($w=0; $w<9; $w++){
    echo $row.') ';
    $row=$row+1;
    echo $num1.'/'.$den1.' + '.$num2.'/'.$den2.'<br>';
    $num1=$num1+1;
    
    for($x=0; $x<9; $x++){
    	echo $row.') ';
    	$row=$row+1;
    	echo $num1.'/'.$den1.' + '.$num2.'/'.$den2.'<br>';
    	$den1=$den1+1;
    
    	for($y=0; $y<9; $y++){
    		echo $row.') ';
    		$row=$row+1;
    		echo $num1.'/'.$den1.' + '.$num2.'/'.$den2.'<br>';
    		$num2=$num2+1;
    
    		for($z=0; $z<9; $z++){
    			echo $row.') ';
    			$row=$row+1;
    			echo $num1.'/'.$den1.' + '.$num2.'/'.$den2.'<br>';
    			$den2=$den2+1;
    		}
    	}
    }
    }
    ?>
    

    The problem is it parse all the way to

    7380) 10/82 + 730/6561.

    Where did it go wrong? Thanx in advance...

  9. Solved it... I guess the problem is because am trying to store it in a variable...

    But i changed to to

    echo nl2br("$summary");
    

    But am curious tho... Is there anyway to store it in a variable without causing those errors?

  10. Hi...

    I read on php.net manual that its better to use str_replace than preg_replace. I used

     

    $summary = clean($_POST['summary']);
    
    $summary = nl2br($summary);
    

    to convert the carriage return(is that the correct term?) to insert them into mysql. So naturally, i want them converted into <br> when i pull them from mysql.

     

    I used this:

    $num_rows=mysql_num_rows($result);
    	if($num_rows == 0){
    	}
    	else{
    		for($x = 0; $x < $num_rows; $x++){
    		$row = mysql_fetch_assoc($result);
    		$id = $row['id'];
    		$position = $row['position'];
    		$summary = $row['summary'];
    
    $order   = array("\r\n", "\n", "\r");
    $replace = '<br />';
    $newstr = str_replace($order, $replace, $summary);
    

     

    But i got this error:

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/...php on line 310
    

    Is it a syntax error?

     

    Thanx in advance...

  11. I tried this

    <?php
    $string = 'what do u get 1/2?';
    $pattern = '/(\d+) (/) (\d+)/i';
    $replacement = '$1 | $3';
    echo preg_replace($pattern, $replacement, $string);
    ?>
    

    but i didn't work...

    Nothing came out :-[

  12. Hi guys,

    I have been using the "CREATE TABLE IF NOT EXISTS" with no problem until today... It keeps on giving me error. So i logged in to my phpMyAdmin to test it out on the sql console and it responded with

    #1064 - 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 '"CREATE TABLE IF NOT EXISTS `email@email.com` ( `id` INT( 7 ) NOT NULL AUTO_IN' at line 1
    

     

    this is the syntax that i used on phpMyAdmin:

    "CREATE TABLE IF NOT EXISTS `email@email.com` (
    	`id` INT( 7 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    	`ref` VARCHAR ( 200 ) NOT NULL ,
    	`date` VARCHAR(10) NOT NULL ,
    	`time` VARCHAR(10) NOT NULL ,
    	`action` TEXT  ,
    	`score` VARCHAR(4),
    	`credit_charge` INT( 4 ) NOT NULL ,
    	) ENGINE = innodb;";
    

     

    i referred to the error that phpMyAdmin returned (error #1064) and found out it highly likely the usage of reserved keyword (http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html). But i cannot find any reserved keyword that i could've used in my query so far...

     

    Any ideas?

  13. If i would like to add this functionality to my app,

    1.how do i go about installing?

    2. Are the tutorials for this?

    3. Could it be done on my localhost?

    4. Could it be applied to a shared server without having to get the administrator to do it fer u?

     

    I'm really a noob so i do apologise if i sound stupid...

    Thanx guys...

  14. The filetype has nothing to do with the error.

     

    The problem is that this: $fileUpload = $_POST['fileUpload']; references an array index that doesn't exist. The data related to the file will be in the $_FILES array, not the $_POST array. Add the following at the top of the ulf-exec.php script to see the structure of both arrays:

     

    echo '<pre>';
    print_r($_POST);
    print_r($_FILES);
    echo '</pre>';
    

    Thanx Pikachu... solved it... but its not over yet... i now need to script the streaming of the pdf from server...

    Is there anyway we can calculate the max the pdf size should be to avoid straining the server?

  15. Hi guys,

     

    This is my first time to insert PDF into MySQL BLOB.

    Below is my form that i used

     

    <?php
    <form enctype="multipart/form-data" name="frmUploadFile" action="ulf-exec.php" method="post">
    
    <select name="title"  id="title">
                          <option>xxx</option>
                          <option>yyy</option>
                          <option>zzz</option>
                        </select>
                      </label>
    <input name="des" type="text" class="dropdownlists1" id="des"></td>
    <input name="fileUpload" type="file" class="dropdownlists1" id="fileUpload" size="20" border=""></td>
    <input type="submit" name="button" id="button" value="Submit">
    
    </form>
    ?>	
    

    I have prepared my database based on the required but decided to test with echo just to confirm there's no issue with the code

    The action="ulf-exec.php" :

    <?php
    
    function clean($str) {
    	$str = @trim($str);
    	if(get_magic_quotes_gpc()) {
    		$str = stripslashes($str);
    	}
    	return mysql_real_escape_string($str);
    }
    
    //Sanitize the POST values
    $title = clean($_POST['title']);
    $des = clean($_POST['des']);
    $fileUpload = $_POST['fileUpload'];
    
    if(empty($des) || $fileUpload == "none")
    
    die("You must enter both a description and file");
    
    
    $fileHandle = fopen($fileUpload, "r");
    $fileContent = fread($fileHandle, $fileUpload_size);
    $fileContent = addslashes($fileContent);
    
    
    $date = date('d').'-'.date('m').'-'.date('y');
    $time = date('h').':'.date('i').':'.date('s');
    
    echo "<h1>File Uploaded</h1>";
    
    echo "The details of the uploaded file are shown below:<br><br>";
    
    echo "<b>File name:</b> $fileUpload_name <br>";
    
    echo "<b>File type:</b> $fileUpload_type <br>";
    
    echo "<b>File size:</b> $fileUpload_size <br>";
    
    echo "<b>Uploaded to:</b> $fileUpload <br><br>";
    
    echo "<a href='uploadfile.php'>Add Another File</a>";
    ?>
    

     

    This is the error:

    Warning: fopen() [function.fopen]: Filename cannot be empty in /ulf-exec.php on line 30
    
    Warning: fread(): supplied argument is not a valid stream resource in ulf-exec.php on line 31
    

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