April 30, 2012

Multiple-choice picture quiz for young students

Question by Sean

I’m trying to make a quiz with multiple submit buttons. I want a picture to appear with ten buttons. Each time the test-taker presses a button I want the value to be posted and the picture to change. So far I’ve tried three approaches:-

1.Using onclick and javascript I could get all the animation done but the values didn’t get posted. From Google I get the impression I’d have to use more javascript to submit the values?

2.Using variables in php the first picture would show and first value would post but then nothing happened, adding a loop meant that all the pictures appeared on top of the other without waiting for any buttons to be clicked…

3.I tried doing separate html pages for each picture, the value then gets posted to a php file, which does $score++; and goes to the next html file, but then the $score isn’t right. Because it’s a local variable? Would this mean I’d have to hold the value in MySQL to get at it? Also this seems a very wasteful way to program the whole thing…

Here is the code for no. 2:

<?php
include 'header.php';

if ($round==1) {$ans='Banana';}
if ($round==2) {$ans='Book';}
if ($round==3) {$ans='Pencil';}
...and so on

if ($_POST['submit']==$ans) {$score++; echo "Right!"; $round++; }
else
{$round++;}
?>

<html>
<form action="index.php" method="post">

<input type="submit" class="button1"  name="submit" value="Banana">
<input type="submit" class="button2" name="submit" value="Balloon">
<input type="submit" class="button3" name="submit" value="Dog">
<input type="submit" class="button4" name="submit" value="Mouse">
<input type="submit" class="button5" name="submit" value="Chair">
<input type="submit" class="button6" name="submit" value="Twelve">
<input type="submit" class="button7" name="submit" value="Pen">
<input type="submit" class="button8" name="submit" value="Book">
<input type="submit" class="button9" name="submit" value="Ball">
<input type="submit" class="button10" name="submit" value="Elephant">
</form>
</html>

<?php

if($ans=="Banana")
{   
echo '<img src="Banana.png" id="picture" class="picture" height="600" width="600" alt="script.aculo.us" />';
}
if($ans=="Pencil")
{   
echo '<img src="Pencil.png" id="picture" class="picture" height="600" width="600" alt="script.aculo.us" />';
}
{   
echo '<img src="Book.png" id="picture" class="picture" height="500" width="500" alt="script.aculo.us" />';
}
...and so on

include 'footer.php';

?>

Any help would be greatly appreciated 🙂

Answer by Starx

Among the approaches, the third one is better than others. Generally on MCQ pages, the question are kept or separate pages. The problem with your local variable can you solved if you use sessions

$_SESSSION['score'] += $score
echo $_SESSION['score'];

using values from one mysql function for a second function

Question by rob melino

I am trying to query my database and create values for later use in another function. My first function (get_users()) should query the requests database and find all users listed for the specific global_id – there will always be a maximum of 4 users for this query. Then I want to use a second function (get_results()) and insert the values that were retrieved from the first function (get_users()) into the second function. In other words, i need to put users1,2,3,4 into get_results($user1, $user2, $user3, $user4) in the second function.

Hoping someone can help! Here are my functions:

        function get_users($global_id)
    {
        $result = mysql_query("SELECT user_purchased FROM requests WHERE global_id = '$global_id'");
        $row = mysql_fetch_row($result);
        $user1 = $row[0];
        $user2 = $row[0];
        $user3 = $row[0];
        $user4 = $row[0];
    }
    function get_results($user1, $user2, $user3, $user4)
    {
        $result = mysql_query("SELECT * FROM results WHERE username != '$user1'
        AND username != '$user2'
        AND username != '$user3'
        AND username != '$user4'
        ORDER BY distance");
        ...more stuff to do here with the query
    }

Thanks

Answer by Hamurabi

Call the second function inside the first one:

function get_users($global_id)
    {
        $result = mysql_query("SELECT user_purchased FROM requests WHERE global_id = '$global_id'");
$count = 0;        
while($row = mysql_fetch_array($result))
        {
        $user[$count] = $row;
        $count++;
        }
    get_results($user[0],$user[1],$user[2],$user[3]);
    }

    function get_results($user1, $user2, $user3, $user4)
    {
        $result = mysql_query("SELECT * FROM results WHERE username != '$user1'
        AND username != '$user2'
        AND username != '$user3'
        AND username != '$user4'
        ORDER BY distance");
        ...more stuff to do here with the query
    }

You can even simplify the get_results function to have one variable as an array instead of 4 varialbles

function get_results($users)
        {
            $result = mysql_query("SELECT * FROM results WHERE username != '".$users[0]."'
            AND username != '".$users[1]."'
            AND username != '".$users[2]."'
            AND username != '".$users[3]."'
            ORDER BY distance");
            ...more stuff to do here with the query
        }

And you should call it like this in the first function

get_results($users);

Answer by Starx

Send the values as parameter to the another function.

function get_users($global_id)
{
    $result = mysql_query("SELECT user_purchased FROM requests WHERE global_id = '$global_id'");
    $row = mysql_fetch_row($result);
    $user1 = $row[0];
    $user2 = $row[0];
    $user3 = $row[0];
    $user4 = $row[0];

    //now send
   get_results($user1, $user2, $user3, $user4);
}

Binary Search Trees, how do you find maximum?

Question by Rachel Moss

I’ve been working with Binary Search Trees in my spare time, and I want to be able to delete nodes from a tree.

In order to get this to work, I need to find the maximum value. How do you go about doing that? Pseudo-code or hints would be appreciated. I’m stuck and not exactly sure how to even begin this.

Answer by Starx

A simple pseudocode would be this. It it is indepandant to binary search I think.

int maxi = 0
foreach(array as item) // or any other loop
    if item>maxi then maxi = item

how to add jquery-fileupload plugin

Question by ALi

I have used carrierwave and followed the link
But not able make it like demo of jquery-fileupload link

Can anyone guide me where to place the files of js file and images and how to include javascript in html.

if any tutorial or example of Rails, that will be great…
Thanks

Answer by Starx

Different people have different opinion about where it is OK to keep the files. Generally,

  1. Javascript are kept in js or script
  2. Images are kept in img or images or media

Including JS on the HTML is done as this

<script type='text/javascript' src='path/to/jsfile.js'></script>

How do I pass a javascript variable to php and reload a frame

Question by bisslad

Ok this may sound a little weird but what i have is a frameset generated by php with codeigniter framework.

In one frame i am displaying rows of database query results with links attached to the row index of each record.

What I want to happen, is that you click one of the links and it brings up a more detailed summary of the record in the right hand frame.

While I am aware of the fact that php is server side etc. i was trying to make the link reload the right hand frame with the value of the index from the left hand side, then within the controller i can query the database using the index and push back the same summary page with the full record.

Answer by Starx

You need to send an AJAX request to the server with the variable. You can google on How to send ajax request to server.

However, Here is a simple example of this, using jQuery

$.post("/next/page", {
   'variable1' : 'value1'
}, function(data) {
   //on success handler
});

Simple slideshow with navigation overlay

Question by Willard

I’m currently re-designing my portfolio site & I would like to use the best non-flash method possible to create the following slideshow shown in my layout:

http://oi49.tinypic.com/xeifeq.jpg

I would only have 3 slides or so in the show, and I would like to overlay left/right nav buttons as shown.

So how do I go about this?

Answer by Starx

There are tons of options. With javascript libraries like jQuery, a lot of animations that needed flash once, can be easily done.

Try googling, jQuery Sliders

php display blank and error on mysqli_fetch_array

Question by raindrop

I receive a blank page when I try connect to mysql, based on the error. php doesn’t like the 3rd line

$link = "SELECT * FROM `Table` WHERE `id`='$id'";
$results = mysqli_query($db,$link);
$qdata = mysqli_fetch_array($results);

This code is working on my old server, so mysqli_fetch_array seems absolute? Or did I miss something else on my php config?

Thanks!

This is the error message
Fatal error: Call to undefined function mysqli_connect() in /opt/php_script/connect.php on line 14

Line 14 is

$link = "SELECT * FROM `Table` WHERE `id`='$id'";

Answer by Starx

Your mysqli extendion is not enabled. Check your php.ini, go to the extensions part and enable it.

Trimming hashed password (for user identification)

Question by GARR

http://www.vidyasocks.com/forums.php?id=1&id=1

as you can see at the bottom I am using hash as a way to identify users. How can I trim off the rest after 10 characters?

Answer by Jason Fuerstenberg

Trimming runs the risks of hash collisions where two users potentially share the same trimmed portion.

http://en.wikipedia.org/wiki/Collision_(computer_science)

Hashing algorithms go to great length to avoid this possibility so modification of the hashed results in any way is not recommended.

Answer by Starx

Use substr():

$hash = substr($hash,0,10);

Eliminating warning in HTML

Question by user1221330

I am creating 3 jsp pages. There are 4 boxes with different id in each pages (i.e. box1, box2, box3, box 4 in page 1; box 5, box6, box7, box8 in page 2; box9, box10, box11, box12 in page 3). Below is the sample code in page 1:

<div class="dragableBox" id="box1">CAT</div>
<div class="dragableBox" id="box2">DOG</div>
<div class="dragableBox" id="box3">HORSE</div>
<div class="dragableBox" id="box4">TIGER</div>

In each page there is also a script. In the script I deliberately use all those ids above as parameters of a function. Below is the sample code in page 1:

dragDropObj.addSource('box1',true);
dragDropObj.addSource('box2',true);
dragDropObj.addSource('box3',true); 
dragDropObj.addSource('box4',true); 
dragDropObj.addSource('box5',true); 
dragDropObj.addSource('box6',true); 
dragDropObj.addSource('box7',true); 
dragDropObj.addSource('box8',true); 
dragDropObj.addSource('box9',true); 
dragDropObj.addSource('box10',true);    
dragDropObj.addSource('box11',true);    
dragDropObj.addSource('box12',true);    

I must do this because as far as I know this is the only way for my program to work. The problem I encounter is that each time the program started, a warning appears:
“The source element with id box5 does not exist”

Although the program still works fine with this warning, I still want to eliminate the warning.

My question here is:
How can I stop such warning from appearing?
Is there a kind of error catching method in HTML?

Answer by Starx

Check the existence of element before adding them.

if (typeof document.getElementById('box5') !== 'undefined'){
    // continue
}
...

Please fill the form - I will response as fast as I can!