June 20, 2013

Access an internally used list

Pulz’s Question:

I’m coding the Dijkstra algorithm in Java. My first method

public void populateDijkstraFrom(Node startNode)

creates a Linked List of nodes with its respective distances and predecessors. My second method

public List<Node> getShortestPath(Node startNode, Node targetNode)

is supposed to create a list of nodes with the shortest path from the startNode to the targetNode using the list of nodes from my populateDijkstraFrom method.

However, I don’t know how to access the list of nodes from my Dijkstra method in the getShortestPath method. I could change the return type from void to LinkedList but I was told that it works using void.

How do I do it?

Thanks

There are basically two ways of solving this.

Easiest one would be return the list of nodes on your method

public List<Node> populateDijkstraFrom(Node startNode) {
       // ^ Configure to return list instead of void

    // ......
    return nodeList;
}

public List<Node> getShortestPath(Node startNode, Node targetNode) {
    List<Node> pdList = populateDijkstraFrom(startNode);
    // ^ --- Get the list by simply passing the same parameter to the method
}

Another is stated by Gian

May 9, 2013

What is the best way to prepend a conjunction to the end of an imploded array in PHP?

Danronmoon’s Questions:

Let’s say I have an array.

$shopping_list = array('eggs', 'butter', 'milk', 'cereal');

What is the easiest and/or most clever way to display this as a comma-delimited list and prepend a word (conjunction, preposition, etc.) to the last value? Desired results include:

'eggs, butter, milk, and cereal'
'eggs, butter, milk, or cereal'
'eggs, butter, milk, with cereal'
'eggs, butter, milk, in cereal'
// etc.

The array will be of variable length, may be associative, and preferably it shouldn’t be modified. It needn’t be imploded either; I figure this is just the standard way of doing things like this. Array dereferencing is fine too, but something PHP 5.3-compatible would be great.

I’m thinking something along the lines of

$extra_word = 'and';

implode(', ', array_slice($shopping_list, 0, count($shopping_list) - 1)) 
    . ', ' . $extra_word . ' '
    . implode(',', array_slice($shopping_list, count($shopping_list) - 1));

But that’s a doozy. Loops are cleaner and slightly less inept:

$i = 0;
$output = '';

foreach ($shopping_list as $item) {
   $i += 1;
   if ($i > 1) {
       $output .= ', ';
       if ($i === count($shopping_list)) {
           $output .= $extra_word . ' ';
       }
   }

   $output .= $item;
}

Both of these ways seem roundabout. Is there a better way out there that comes to mind?

This is cleaner too.

$pop = array_pop($shopping_list);
echo implode(", ", $shopping_list)." and $pop.";
February 18, 2013

Align width of container div to the sum of floating div

Question by Seb37

I have the following html:

<div class="main_container">
    <div class="sub_container">
        <div class="floating">wookie1</div>
        ...
        <div class="floating">wookie5</div>
    </div>
</div>

I want sub_container to have the exact width of the sum of the floating div.

If I use “display:table;” for sub_container and “display: inline-block;” for floating divs, it works fine:

enter image description here

until I have enough div in the list, so that the sum of width is larger than main_container and they break on the next line:

enter image description here

But still, I want subcontainer (yellow background) to to be ALWAYS the EXACT WIDTH of the sum of the divs, even when they go on several lines, like this:

enter image description here

I’ve googled for hours now, and wasn’t able to find an elegant solution (css only, if possible.)

Here’s the jsfiddle, to play with this.

Answer by Starx

I got bored trying this and created a JS script based on jQuery to solve it.

var t = $(".sub_container").width()/$(".floating").outerWidth();
$(".sub_container").width(parseInt(t) * $(".floating").outerWidth());

Demo

April 20, 2012

Dynamic Select list using sql data

Question by Baruch

I am trying to create a select drop-down list using php. Every time i try, i get an error.
Here is my code:

The function:

function dropDown(){


$options="<select>"; 
$connect = mysql_connect('localhost','id','pass') or die ("couldn't   connect!").mysql_error; 

mysql_select_db('db') or die('could not connect to database!');


$sql="SELECT * FROM DESC"; 
$result=mysql_query($sql); 


while ($row=mysql_fetch_array($result)) {  ****this is line 60

$name=$row["name"]; 

$options.="<option value="$name">".$name."</option>"; 
} 

$options.= "</SELECT>";
return "$options";
}

and then i just call it in my code

<?php  
 include ('includes/functions.php');
....



$list = dropDown();
echo "$list";


....
>

the error i get is:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/garagenj/public_html/dispatch/includes/functions.php on line 60

Answer by JT Smith

If your table name is DESC then that’s your problem. It’s all about namespacing. You can’t even have a field named desc, I’ve tried. It errors everytime

Answer by Starx

SELECT * FROM DESC ??????

  • From what table
  • Or, if the table name is DESC. escape them with ticks

And You dont have start a new connection, every time you call the function. Put your connection part somewhere else

$connect = mysql_connect('localhost','id','pass') or die ("couldn't   connect!").mysql_error; 
mysql_select_db('db') or die('could not connect to database!');

function dropDown(){
    $options="<select>"; 
    $sql="SELECT * FROM `DESC`"; 
    $result=mysql_query($sql); 

    while ($row=mysql_fetch_array($result)) { 

        $name=$row["name"]; 

        $options.="<option value="$name">".$name."</option>"; 
    } 

    $options.= "</SELECT>";
    return $options;

}
April 5, 2012

missing ) after argument list on line 1

Question by Rafael

 <script type="text/javascript">$('#multipleResults').show(); $('#multipleResultsOut').html('<table><tr><td colspan="2"><?php echo $lang['search.limited']; ?></td></tr><?php while ($bQr = mysql_fetch_assoc($botquery22)) { echo ('<tr><td style="width: 40px;"><div style="width: 40px; height: 40px; overflow: hidden; background-position: center; background-repeat: no-repeat; background-image: url('%www%/images/badges/'.$bQr['name'].'');"></div></td><td><a href="#" onclick="getBadge('%www%/', ''.$bQr['name'].''); return false;">'.$bQr['badgename'].'</a><br />'.$bQr['desc'].'</td></tr>'); } ?><?php echo $end; ?>;</script><?php echo $lang['search.x.results']; ?>

And I’ve rolled the code again, but still the same error.

Answer by Starx

May be the problem of quotes, the php snippets are also using single quotes as your js script.

Try them this way

$('#multipleResults').show();
$('#multipleResultsOut').html('<table><tr><td colspan="2">'+
<?php echo $lang['search.limited ']; ?>+'</td></tr>'+
<?php while ($bQr = mysql_fetch_assoc($botquery22)) { 
    echo (' < tr > < td style = "width: 40px;" > < div style = "width: 40px; height: 40px; overflow: hidden; background-position: center; background-repeat: no-repeat; background-image: url('%www%/images/badges/'.$bQr['name'].'');" > < /div></td > < td > < a href = "#" onclick = "getBadge('%www%/', ''.$bQr['name'].''); return false;" > '.$bQr['
badgename '].' < /a><br / > '.$bQr['
desc '].' < /td></tr > '); 
} 
?>
<?php echo $end; ?>;
...

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