Jump to content

Recommended Posts

Why would the following code return only single digits for $id? The values for id in the database are 0, 2, 3, 4, 5, 6, 7, 44, 45, but the below code only returns single digits. I'm new to arrays so I'm guessing I'm making some kind of mistake there, but I don't know.

 

<?
	$count = 0;
	$query1 = "SELECT * FROM scheduled_events WHERE date != 'title' ORDER BY pos";
	$result1= mysql_query($query1) or die("Could not perform query: ".mysql_error());
	while ($row1 = mysql_fetch_array($result1)){
		$date[$count] = $row1['date'];
		$event[$count]= $row1['event'];
		$pos[$count] =  $row1['pos'];
		$id[$count] = $row1['id'];
		$count++;
		$count_max = $count;
	}
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/115923-solved-a-real-puzzle-for-me/
Share on other sites

I don't see anything wrong with your code either, but it can be written a little simpler:

<?php
	$date = array();
                $event = array();
                $pos = array();
                $id = array();
	$query1 = "SELECT * FROM scheduled_events WHERE date != 'title' ORDER BY pos";
	$result1= mysql_query($query1) or die("Could not perform query: ".mysql_error());
	while ($row1 = mysql_fetch_assoc($result1)){
		$date[] = $row1['date'];
		$event[]= $row1['event'];
		$pos[] =  $row1['pos'];
		$id[] = $row1['id'];
	}
                $count_max = count($date);
?>

 

Ken

To answer what is displayed, always the left most digit. If I select the item with the id 44, then it returns 4. The id 45 does the same. When I changed the 7 to a 71 manually, the script only displayed the 7. Here is the full code:

<body bgcolor="cccccc">
<?
	$count = 0;
	$query1 = "SELECT * FROM scheduled_events WHERE date != 'title' ORDER BY pos";
	$result1= mysql_query($query1) or die("Could not perform query: ".mysql_error());
	while ($row1 = mysql_fetch_array($result1)){
		$date[$count] = $row1['date'];
		$event[$count]= $row1['event'];
		$pos[$count] =  $row1['pos'];
		$id[$count] = $row1['id'];
		$count++;
		$count_max = $count;
	}
?>
<table border="0" cellpadding="0" cellspacing="0" width="375" style="font-family: helvetica; font-size: 9pt;">
<tr>
	<td colspan="3"><b>Instructions:</b></td>
</tr>
<tr>
	<td height="5"></td>
</tr>
<tr>
	<td colspan="3">Select “Edit” to alter an entry or “Delete” to delete it. Or select “Add Entry” at the bottom to add to scheduled events. </td>
</tr>
<tr>
	<td height="15"
</tr>
<tr>
<?
$count = 0;
while ($count < $count_max){
	print "<td width=\"75\" style=\"font-size: 8pt;\"><a href=\"manage_events_edit1.php?id=".$id[$count]."\" target=\"manage1\"><u>Edit</u></a> / <a href=\"manage_events_delete.php?id=".$id[$count]."\" target=\"manage1\"><u>Delete</u></a></td>";
	print "<td style=\"font-size: 8pt;\">".$pos[$count]."- </td>";
	print "<td style=\"font-size: 8pt;\">".$event[$count]."</td>";
	print "</tr><tr><td height=\"15\"></td></tr><tr>";
	$count++;
}
?>
</tr>
<tr>
	<td height="10"
</tr>
<tr>
	<td><b>- OR -</b></td>
</tr>
<tr>
	<td height="10"></td>
</tr>
<tr>
	<td colspan="3"><a href="manage_events_add1.php"><img src="resources/add_entry.jpg" border="0"></a></td>
</tr>
</table>

 

 

your retrieving the values from mysql, is that length set to only 1 digit by any chance, thats the only possible reason i can see,

 

oh yeah you have a few formatting errors such as:

<body bgcolor="cccccc">

should be

<body bgcolor="#cccccc">

 

and you need to close this:

 

<tr>
	<td height="15"
</tr>

Okay.. test 2

let see what we are getting back from the query

<?php
	$count = 0;
	$query1 = "SELECT * FROM scheduled_events WHERE date != 'title' ORDER BY pos";
	$result1= mysql_query($query1) or die("Could not perform query: ".mysql_error());
	while ($row1 = mysql_fetch_array($result1)){
		echo "#$count#"; //ADD (only to help find it)
		print_r($row1); //ADD
		$date[$count] = $row1['date'];
		$event[$count]= $row1['event'];
		$pos[$count] =  $row1['pos'];
		$id[$count] = $row1['id'];
		$count++;
		$count_max = $count;
	}
?>

The output was:

 

#0#Array ( [0] => 2 [id] => 2 [1] => June 16 [date] => June 16 [2] => ITHF Board of Directors Meeting [event] => ITHF Board of Directors Meeting [3] => 2 [pos] => 2 [4] => [take] => ) #1#Array ( [0] => 3 [id] => 3 [1] => June 17 [date] => June 17 [2] => ITHF Annual Membership Breakfast Meeting [event] => ITHF Annual Membership Breakfast Meeting [3] => 3 [pos] => 3 [4] => [take] => ) #2#Array ( [0] => 4 [id] => 4 [1] => June 14-18 [date] => June 14-18 [2] => ITHF's booth #235 in the House of Friendship [event] => ITHF's booth #235 in the House of Friendship [3] => 4 [pos] => 4 [4] => [take] => ) #3#Array ( [0] => 5 [id] => 5 [1] => June 6-19, 2009 [date] => June 6-19, 2009 [2] => Register Now for ITHF's Tour of Britain and Ireland [event] => Register Now for ITHF's Tour of Britain and Ireland [3] => 5 [pos] => 5 [4] => [take] => ) #4#Array ( [0] => 6 [id] => 6 [1] => Late August [date] => Late August [2] => 3rd Quarterly Newsletter out [event] => 3rd Quarterly Newsletter out [3] => 6 [pos] => 6 [4] => [take] => ) #5#Array ( [0] => 7 [id] => 7 [1] => Nov - Dec [date] => Nov - Dec [2] => Renewal reminders out [event] => Renewal reminders out [3] => 7 [pos] => 7 [4] => [take] => ) #6#Array ( [0] => 44 [id] => 44 [1] => Apr. 1-3 [date] => Apr. 1-3 [2] => This is a test entry [event] => This is a test entry [3] => 8 [pos] => 8 [4] => [take] => ) #7#Array ( [0] => 45 [id] => 45 [1] => July 23 [date] => July 23 [2] => This is another test entry [event] => This is another test entry [3] => 9 [pos] => 9 [4] => [take] => )

 

I broke it up so its easier to read:

 

#0#Array ( [0] => 2 [id] => 2 [1] => June 16 [date] => June 16 [2] => ITHF Board of Directors Meeting [event] => ITHF Board of Directors Meeting [3] => 2 [pos] => 2 [4] => [take] => )

 

#1#Array ( [0] => 3 [id] => 3 [1] => June 17 [date] => June 17 [2] => ITHF Annual Membership Breakfast Meeting [event] => ITHF Annual Membership Breakfast Meeting [3] => 3 [pos] => 3 [4] => [take] => )

 

#2#Array ( [0] => 4 [id] => 4 [1] => June 14-18 [date] => June 14-18 [2] => ITHF's booth #235 in the House of Friendship [event] => ITHF's booth #235 in the House of Friendship [3] => 4 [pos] => 4 [4] => [take] => )

 

#3#Array ( [0] => 5 [id] => 5 [1] => June 6-19, 2009 [date] => June 6-19, 2009 [2] => Register Now for ITHF's Tour of Britain and Ireland [event] => Register Now for ITHF's Tour of Britain and Ireland [3] => 5 [pos] => 5 [4] => [take] => )

 

#4#Array ( [0] => 6 [id] => 6 [1] => Late August [date] => Late August [2] => 3rd Quarterly Newsletter out [event] => 3rd Quarterly Newsletter out [3] => 6 [pos] => 6 [4] => [take] => )

 

#5#Array ( [0] => 7 [id] => 7 [1] => Nov - Dec [date] => Nov - Dec [2] => Renewal reminders out [event] => Renewal reminders out [3] => 7 [pos] => 7 [4] => [take] => )

 

#6#Array ( [0] => 44 [id] => 44 [1] => Apr. 1-3 [date] => Apr. 1-3 [2] => This is a test entry [event] => This is a test entry [3] => 8 [pos] => 8 [4] => [take] => )

 

#7#Array ( [0] => 45 [id] => 45 [1] => July 23 [date] => July 23 [2] => This is another test entry [event] => This is another test entry [3] => 9 [pos] => 9 [4] => [take] => )

 

Thats very weird, are you sure the print_r ($id) had the same problem?,

is this page live ?

 

The print_r ($id) still returned the same problem. I have manually checked the values in the database. They are 2, 3, 4, 5, 6, 7, 44, 45. I don't understand how the values could be in the database, but only pull the first digit.

 

 

Apparently the solution was to change $id[$count] in each occurence to $tid[$count] as I had $id used in my SESSION script not herein posted. I'm should have posted the entire code. For those people whose time I wasted by not posting the full code, I apologize!

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.