logo
down
shadow

PHP Show foreach() results from 2 SQLite-arrays, one as random


PHP Show foreach() results from 2 SQLite-arrays, one as random

By : Rejinald
Date : November 18 2020, 01:01 AM
this will help You can fall a function inside the first foreach a pass category of the article to that function. Now in that functino collect all images in an array related to the passed category and then return it by randomizing it. Code below ..
code :
<?php foreach($res as $article): ?>
    <?php

    $img = get_img($article['category']);

    ?>

    <div class="artikelContent">
        <?php echo $article['title']; ?><br>
        <?php echo $article['content'];?><br>
        <img src="<?php echo $img; ?>" /> <!-- The return result will be shown here -->
        <div class="floatRight clear"><?php echo "Artikel skriven " . $article['author'] . " " . $article['pubdate']; ?></div>
    </div>

<?php endforeach; ?>

<?php

function get_img($category){


    $stmt2 = $db->prepare('SELECT * FROM Object WHERE category = '.$category);
    $stmt2->execute();
    $res2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);
    $rnd = array();
    foreach($res2 as $object){
        $rnd[] = $object['img'];
    }

    $n = rand(0,(count($rnd)-1));

    return $rnd[$n];

}


?>


Share : facebook icon twitter icon
Sqlite query with random values, why do I get always the same results?

Sqlite query with random values, why do I get always the same results?


By : Guerro
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Here is my code, I try to perform random queries on my Sqlit databse. , OK, Solved the issue, please see the new code:
code :
int i2=gerRank(livello);
    Random r = new Random();
    int i1 = r.nextInt(i2);
    Cursor parolaCursor = db.getReadableDatabase().rawQuery(
            "SELECT _id, english, korean, difficulty FROM ordinato WHERE difficulty < "
                    + Integer.toString(i1)+ " ORDER BY difficulty DESC", null);
    parolaCursor.moveToFirst();     
foreach of arrays extracted from XML documents, I'm getting repeated results too. How to fix this?

foreach of arrays extracted from XML documents, I'm getting repeated results too. How to fix this?


By : user3523324
Date : March 29 2020, 07:55 AM
may help you . Your problem is the $y_slottype variable (among others). In your current implementation, this variable is assigned when you encounter an tag with the key slotType. However, it is never reset in the case that no respective tag is found.
In other words: when an item does not have an tag with key="slotType", the $y_slottype variable will still have its value from the previous iteration. One solution would be to reset the variable at the beginning of each iteration:
code :
foreach($f_item as $f_itemid => $f_itemamount){
    $y_weight = NULL;
    $y_itemtype = NULL;
    $y_range = NULL;
    $y_slottype = NULL;
    // ...
    foreach($f_item_attr as $f_attr_name => $f_attr_value){
        if($l_attr_id == "weight")
            $y_weight = $f_attr_value;
        if($l_attr_id == "slotType")
            $y_slottype = $f_attr_value;
        if($l_attr_id == "itemType")
            $y_itemtype = $f_attr_value;
        if($l_attr_id == "range")
            $y_range = $f_attr_value;
    }
How to use 2 arrays in a foreach loop PHP for choosing random element

How to use 2 arrays in a foreach loop PHP for choosing random element


By : divyananjan
Date : March 29 2020, 07:55 AM
this will help You messing between array variable and their element.
First, you cannot do echo $array1; as the variable is array and echo is for string. Second, foreach($array2 as $array2) is reassign $array2 as both element so the original array is mess-up.
code :
foreach(range(1,10) as $v) {
    echo $array1[array_rand($array1)] . PHP_EOL;
}
par with foreach results random order but not with map

par with foreach results random order but not with map


By : user3611637
Date : March 29 2020, 07:55 AM
will be helpful for those in need Conceptually we differentiate between temporal and spatial order. Parallel collections execute operations out-of-order in the temporal sense, however in the spatial sense they are in-order. Ssemantics state
Show random results 50% of the time, other 50% show another result?

Show random results 50% of the time, other 50% show another result?


By : Argenis Ascanio
Date : March 29 2020, 07:55 AM
seems to work fine Just generate a random number between 0 and 1 and check whether it is above 0.5.
In JavaScript you can generate a random number between 0 and 1 using Math.random(). Comparing that number to 0.5 can be done using the "greater than" > operator.
code :
function shouldShowAlert() {
  return Math.random() > 0.5
}
if (shouldShowAlert()) {
  // Show your alert
}
Related Posts Related Posts :
  • New to PHP, trying to extract information from another website
  • JavaScript AmChart to Image for Email
  • Is javascript validation enough to keep my forms secure?
  • Regex extract variables from [shortcode]
  • Download a .mp3 link and save it to computer with PHP
  • Error with the ereg_replace
  • Getting the fields attached to a bundle of an entity in Drupal
  • Php how to check multiple times (within `foreach`) if values from array exists in mysql?
  • MySQL and PHP parsing strange string
  • Pull specific data from multidimensional associative array
  • How to get the original URI extension using PHP Tonic?
  • Upload videos and images from Android to PHP server?
  • Unset a multidimensional array with another multidimensional array with values than key
  • Calling a user e-mail to add to a mailto link PHP
  • Why is override_function coming back as an undefined function
  • PHP mySQLi_fetch_all: iterate through each row
  • Using pow in php
  • What's wrong with my PHP Script? T_String issue
  • how to call a function of another file without including that file
  • is_rgb function in php
  • transform the numbers to letters using php
  • How to catch an exception from another class method PHP
  • PHP exec is blocked by ISP
  • PHP4 to PHP5 migration character encoding. latin1 vs utf8
  • Begin Transaction not defined
  • How much harm can a user do with xss on a page unique to them?
  • How to import data from a php file to javascript variable
  • PHP and Javascript - log complete workflow
  • When I echo !!! 0 it gives me a strange result
  • jquery ajax post dies after submit
  • How to upload file with curl on sftp server
  • Advanced search using a text input to search through only one category
  • Why do I get "Non-static method App\Models\Category::products() should not be called statically" in laravel
  • Stop notices from undefined indexes in arrays
  • PHP Excel Error when entering SUM function to a cell
  • laravel 4 eloquent eager load relation count
  • click submit in html and run php code
  • Extremely frustrating behavior with Internet Explorer, Bootstrap and Respond.js
  • Vkontakte vtag issues
  • Divide the number to a correct decimal
  • mysqli_insert_query not working
  • How to make a sitemap using zend framework 1
  • Laravel: array to Model with relationship tree
  • $_SERVER['DOCUMENT_ROOT'] vs $path = $_SERVER['DOCUMENT_ROOT']
  • What is the best way to store multiple checkbox values in the database and work with it later on
  • Why does foreach copy the array when we did not modify it in the loop?
  • Receive Jquery/Ajax Post Request in PHP
  • How to better use the if(!empty()){ //codes} statement
  • PHP error mysql_close() boolean given
  • Move an image file from one server to another using PHP
  • PHP: Encoding non ISO-8859-1 charactrers into utf8
  • Handling MySQL errors in PHP
  • Object orientated mysql num_rows query in one line
  • Remove not closed html elements from end of text
  • Generate random ENUM value [PHP, MySQL]
  • Deleting data from table using jQuery and SQL
  • How to make sure the constructor is executed when I'm trying to reach a static property?
  • Trying to understand arrow next to PHP function
  • Form using external php file to show "Sucess"
  • WooCommerce - new shipping method - how to query individual product weights
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org