Jump to content

Looping an array


Julian

Recommended Posts

Hi Guys I have to print the array like this:

$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'),
    );

 

Numbers here are days from database, field 'days'.  What I'm trying to do is obtain the days for this array from the database and loop until last day.

$days = array(
     row_days['day'] => array ('/days.php?days=row_days['day']'));

 

Thanks for the help.

Link to comment
Share on other sites

Forget the example...

 

There's a table containing the day in fields, example

 

id  |  days |  month

1  |      4  |  may

2  |      6  |  may

 

In order to display correctly on my calendar the array should be like this:

 

$days = array(

        4=>array('/'),//here goes the page linked

        6=>array('/'),//here goes other page linked 

);

 

I just want to make a query to the DB that loop and make this array like this:

        $sql = "SELECT id_calendar, day FROM calendar ORDER BY id_calendar";
$res = mysql_query($sql) or die(mysql_error());

	$today=>array();	
	while (list($id_calendar, $day) = mysql_fetch_row($res)) {
    	        $new_type_array[$id_calendar] = $day;

Link to comment
Share on other sites

I *think* I understand (not really, but maybe):

 

$sql = "SELECT id_calendar, day FROM calendar ORDER BY id_calendar";
$res = mysql_query($sql) or die(mysql_error());

$today=>array();	
while ($row = mysql_fetch_row($res) {
  $arrayVal = '/weblog/archive/2004/Jan/' . $row[day];
  $today[$row[id_calendar]] = array ($arrayVal);
}

Link to comment
Share on other sites

Thanks for the great help.  I get an error with the code, anyway let me try to explain better what I'm trying to do:

 

Here's how the result should print:

 

$days = array(

        4=>array('/'),//here goes the page linked

        6=>array('/'),//here goes other page linked

);

 

I trying to achieve this with variables obtained from a database like:

 

$days = array(

        $var1=>array('$var2'),

);

Link to comment
Share on other sites

Julian,

 

Your posts are not clear. What is meant by "here goes the page linked" and "here goes other page linked". You say that is how you want them to print out? I'm not understanding if you want to create an array or print something to the screen (which is what your last post stated).

 

The code I posted will create an array as you are asking. If there is a typo either fix it or state what the error is. I have cleaned it up here:

 

<?php
$sql = "SELECT id_calendar, day FROM calendar ORDER BY id_calendar";
$res = mysql_query($sql) or die(mysql_error());

$today=array();

while ($row = mysql_fetch_assoc($res)) {
  $arrayVal = '/weblog/archive/2004/Jan/' . $row[day];
  $today[$row[id_calendar]] = array ($arrayVal);
}

echo "<pre>";
print_r($today);
echo "</pre>";
?>

Link to comment
Share on other sites

I'm very sorry mjdamato.  Your code worked great.

 

Look here:

 

$days = array(

        4=>array('/users.php?day=4')

);

 

This array will print a calendar on screen and show day "4" as a link to /users.php?day=4, and so on.  I stored those days into a database.  What I'm trying to do is a query to database that replace the "4" for a variables obtained from the field "day" on the database and loop in order to get everyday I inserted on the DB.  I hope I explained right this time. 

 

I really appreciate that you spare your precious time with this.  Thanks a lot.

 

Link to comment
Share on other sites

Here's what I have so far:

 

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

 

If I uncomment the "while" I get an error.  I think I'm not doing the loop well.  Maybe someone can help me!  Thanks

Link to comment
Share on other sites

There is nothing wrong with your loop, per say. But I think the script is interpreting the code as if you are trying to put a loop into the array as opposed to creating a loop to add values to the array.

 

I'm confused, you said my code worked, but you apparently still have a problem. I did notice that I had changed some of the variable names because of the code I was testing on. Does this not work for what you want:

 

<?php

$sql = "SELECT id_calendar, day FROM calendar ORDER BY id_calendar";

$res = mysql_query($sql) or die(mysql_error());

 

$days = array();

 

while ($row_calendar = mysql_fetch_assoc($res)) {

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

  $days[$row_calendar[id_calendar]] = array ($arrayVal);

}

?>

Link to comment
Share on other sites

Mr, mjdamato the code you posted worked but I can't change the syntax because the script won't work.

 

I don't follow you. It worked but it doesn't work? What doesn't work? Are you getting error messages or incorrect output. Be specific please.

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.