Jump to content

[SOLVED] Fill the array from a database-request


Julian

Recommended Posts

Hi guys.  I posted this problem I'm having but I think I didn't explain well.  Here's the problem:

 

<?php
$days = array(
    2=>array('/weblog/archive/2004/Jan/02','linked-day'),
    3=>array('/weblog/archive/2004/Jan/03','linked-day'),
    8=>array('/weblog/archive/2004/Jan/08','linked-day'),
    22=>array('/weblog/archive/2004/Jan/22','linked-day'),
);
?>

 

I have to fill the array from a database-request loop in that exact format.  Here's what I did:

 

$days = array(
	while (list($row_calendar) = mysql_fetch_assoc($calendar)) {
	$row_calendar['dia'] => array('/weblog/archive/2004/Jan/'.$row_calendar['dia']),
	}
	);

 

I get this error with this:  parse error, unexpected T_WHILE, expecting ')' .  I think I shouldn't try to do the loop inside the array, maybe someone can help me do the trick. Thanks

 

Link to comment
Share on other sites

I do not think you can put a while loop inside an array definition try this:

 

<?php
$days = array();

while (list($row_calendar) = mysql_fetch_assoc($calendar)) {
	$days[$row_calendar['dia']] = '/weblog/archive/2004/Jan/'.$row_calendar['dia'];
}
?>

 

Should work.

Link to comment
Share on other sites

Dip my bad, you are listing the thing, here try this:

 

<?php
$days = array();
             // row calendar is now an array so the dia reference should work.
while ($row_calendar = mysql_fetch_assoc($calendar)) {
	$days[$row_calendar['dia']] = '/weblog/archive/2004/Jan/'.$row_calendar['dia'];
}
?>

 

Or this might work

<?php
$days = array();

while (list($row_calendar) = mysql_fetch_assoc($calendar)) {
                        // since row_calendar is listed it should not be an array. 
	$days[$row_calendar] = '/weblog/archive/2004/Jan/'.$row_calendar;
}
?>

Link to comment
Share on other sites

Thanks again frost110

 

But still doesn't work.  For example the resulting array should be like this:

<?php
$days = array(
    2=>array('/weblog/archive/2004/Jan/02'),
    3=>array('/weblog/archive/2004/Jan/03'),
    8=>array('/weblog/archive/2004/Jan/08'),
    22=>array('/weblog/archive/2004/Jan/22'),
);
?>

 

I tried both options and I get no results, maybe what I missing here is that the loop result should look exactly as the example above.  Thanks for the help.

 

Link to comment
Share on other sites

Are you sure the query is even running through and is correct?

 

I do not see where the query is ran etc so I cannot say for a fact that it is correct etc.

 

I would check your query via phpMyAdmin or something similiar and make sure that results are being returned.

Link to comment
Share on other sites

Thanks frost110

 

The query is simple

 

	mysql_select_db($database_concierge, $concierge);
$query_calendar = "SELECT * FROM calendar";
$calendar = mysql_query($query_calendar, $concierge) or die(mysql_error());
$row_calendar = mysql_fetch_assoc($calendar);
$totalRows_calendar = mysql_num_rows($calendar);

 

This array will print a link on a calendar for each number that correspond in the DB.  When I use the code you posted I get no errors, but it doesn't work on the array, for example I did this to check the results from the database:

 

$days = array();
	$row_calendar['dia'] = '/weblog/archive/2004/Jan/'.$row_calendar['dia'];
	);

 

It worked, but only show me the last record on the DB, that's why I need to do a loop. 

 

Very appreciated your suggestions.

Link to comment
Share on other sites

I think you are confused, that or I am confused.

 

<?php
$days = array();
             // row calendar is now an array so the dia reference should work.
while ($row_calendar = mysql_fetch_assoc($calendar)) {
	$days[$row_calendar['dia']] = '/weblog/archive/2004/Jan/'.$row_calendar['dia'];
}

print_r($days);
?>

 

What does that print out?

 

The code here

$days = array();
	$row_calendar['dia'] = '/weblog/archive/2004/Jan/'.$row_calendar['dia'];
	);

 

Does not make any sense at all and I do not understand what you are trying to do with it. Maybe if you explain what the logic you are hoping to achieve from that I can help, but yea that should throw a syntax error and would only pull up the last one because you cleared the array and the only portion you are putting into was the last value retrieved from the DB since it is outside the loop.

Link to comment
Share on other sites

let do same debug

1st try

<?php
mysql_select_db($database_concierge, $concierge);
$query_calendar = "SELECT * FROM calendar";
$calendar = mysql_query($query_calendar, $concierge) or die(mysql_error());
// $row_calendar = mysql_fetch_assoc($calendar);
        echo 'Total rows is: ';
echo $totalRows_calendar = mysql_num_rows($calendar);
?>

if output some number greater then 0 try

<?php
mysql_select_db($database_concierge, $concierge);
$query_calendar = "SELECT * FROM calendar";
$calendar = mysql_query($query_calendar, $concierge) or die(mysql_error());
//$row_calendar = mysql_fetch_assoc($calendar);
$totalRows_calendar = mysql_num_rows($calendar);
        $days = array();
while ($row_calendar = mysql_fetch_assoc($calendar)) {
	$days[$row_calendar['dia']] = '/weblog/archive/2004/Jan/'.$row_calendar['dia'];
}
        echo "<pre>\n";
        print_r($days);
        echo "</pre>";
?>

what is output?

Link to comment
Share on other sites

This prints:

 

Array

(

)

 

Only that.

 

The post I did before I had an error.  This is how I can get a result:

$days = array( //do the query within the array

$row_calendar['dia'] = '/weblog/archive/2004/Jan/'.$row_calendar['dia'];

);

 

Link to comment
Share on other sites

If I do a query without the array like this:

 

<?
while (list($row_calendar) = mysql_fetch_assoc($calendar)) {
echo '<br />';
echo $row_calendar['dia'];
echo '<br />';

 

It prints out:

 

10

 

11

 

12

 

13

 

14

Link to comment
Share on other sites

If I do a query without the array like this:

 

<?
while (list($row_calendar) = mysql_fetch_assoc($calendar)) {
echo '<br />';
echo $row_calendar['dia'];
echo '<br />';

 

 

That code does not make sense...you are using the list feature which basically takes the first part of the array and assigns it to the variable...there for the ['dia'] should not work because $row_calendar is just a string not an array...unless you have serialized data which than of course would need to be unserialized...

 

Am I completely missing something here because given the statement above I am completely lost.

Link to comment
Share on other sites

Well I am out of suggestions either way. Given that what sasa and I posted above should work flawlessly unless you are somehow for some reason resetting the $days array AFTER the while loop is ran.

 

Post the full code of what it currently is with the loop assigning data to $days. Just to be sure there are no "mishap" errors in it.

Link to comment
Share on other sites

Original code:

 

<?php
    $days = array(
        2=>array('/weblog/archive/2004/Jan/02'),
        3=>array('/weblog/archive/2004/Jan/03'),
        8=>array('/weblog/archive/2004/Jan/08'),
        22=>array('/weblog/archive/2004/Jan/22'),
    );
    echo generate_calendar(2004, 1, $days, 3);
?>

 

Changed code:

 

<?php
    $days = array();
    while (list($row_calendar) = mysql_fetch_assoc($calendar)) {
    $days[$row_calendar] = '/weblog/archive/2004/Jan/'.$row_calendar;
    }
    echo generate_calendar(2004, 1, $days, 3);
?>

 

Link to comment
Share on other sites

more debug

<?php
mysql_select_db($database_concierge, $concierge);
$query_calendar = "SELECT * FROM calendar";
$calendar = mysql_query($query_calendar, $concierge) or die(mysql_error());

        $days = array();$c = 0;

while ($row_calendar = mysql_fetch_assoc($calendar)) {
                $c++;
	$d = $row_calendar['dia'];
                $l = '/weblog/archive/2004/Jan/'.$d;
                echo "Racord no $c --> dia = $d --> long = $l \n <pre>";
	$days[$d][] = $l;
	print_r($days);
                echo "\n </pre><hr /> \n";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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