/    Sign up×
Community /Pin to ProfileBookmark

autonumbering of variables

I have categories (duplicate listings) that I need to get to single occurances (that’s done), then have each one tied a number starting at 1, stopping at whatever the highest number is in that set. Everything I’ve tried so far doesn’t get the numbers to change. For example, my attempt below outputs just a 1 for every category. Obviously I’m doing something wrong or have the code in the wrong place, but don’t know how I should set it up. Please, help!

[CODE] $catmenu = mysql_query(“SELECT subcategory, category FROM $DBTABLE1 ORDER BY category, subcategory”);
$expcatmenu = mysql_query(“SELECT DISTINCT category FROM $DBTABLE1 ORDER BY category”);
$enumrows=mysql_num_rows($expcatmenu);
for($e = 1; $e <= $enumrows; $e++)
while($row = mysql_fetch_array($catmenu))
{

$CAT=$row[‘category’];
$SUBCAT=$row[‘subcategory’];

if ($CAT != $prev_row)
{
echo “<br><font face=”Arial, Helvetica, sans-serif” size=2><b><a onClick=”expandcontent(‘$e’)”><u>$e $CAT</u></a></b></font><br><div id=”$e” class=”switchcontent”>”;
}
$encCAT=htmlentities($CAT);
$encSUBCAT=htmlentities($SUBCAT);
echo “&nbsp;&nbsp;<font face=”Arial, Helvetica, sans-serif” size=2><a href=”http://www.navi-gator.com/demo/bg-testing/sfm/products.php?cat=$encCAT&subcat=$encSUBCAT”>$SUBCAT</div></a></font>”;
$prev_row=$CAT;
}
[/CODE]

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@TJ111Jan 30.2008 — get rid of the for loop.

[code=php]
$e=1;

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

$CAT=$row['category'];
$SUBCAT=$row['subcategory'];

if ($CAT != $prev_row)
{
echo "<br><font face="Arial, Helvetica, sans-serif" size=2><b><a onClick="expandcontent('$e')"><u>$e $CAT</u></a></b></font><br><div id="$e" class="switchcontent">";
}
$encCAT=htmlentities($CAT);
$encSUBCAT=htmlentities($SUBCAT);
echo "&nbsp;&nbsp;<font face="Arial, Helvetica, sans-serif" size=2><a href="http://www.navi-gator.com/demo/bg-testing/sfm/products.php?cat=$encCAT&subcat=$encSUBCAT">$SUBCAT</div></a></font>";
$prev_row=$CAT;

$e++;
}[/code]
Copy linkTweet thisAlerts:
@stmosaicauthorJan 30.2008 — Got rid of the for loop and implemented your solution, but still getting the #1 for all the categories.
Copy linkTweet thisAlerts:
@stmosaicauthorJan 30.2008 — oops! I put the $e=1;

in the while loop. Put it into it's proper place, but now I'm getting the following output

[B]1[/B] category one

[B]7[/B] category two

[B]14[/B] category three

rather than sequential numbers. They need to be sequential for me to stand a chance of integrating the JS menu expanding & contracting
Copy linkTweet thisAlerts:
@TJ111Jan 30.2008 — Did you add the $e++ at the end of the while loop?
Copy linkTweet thisAlerts:
@stmosaicauthorJan 30.2008 — Sure did. Right after


$prev_row=$CAT;

$e++;

}
Copy linkTweet thisAlerts:
@TJ111Jan 30.2008 — Ok, if you only want for each item being output, put the $e++ inside if the if ($CAT != $prev_row) statement. Then it will only increase when that condition is true.
Copy linkTweet thisAlerts:
@stmosaicauthorJan 30.2008 — That did it. Thanks TJ111!!! Now I might be able to get my expandable contracible menu to work. One step closer!
Copy linkTweet thisAlerts:
@stmosaicauthorJan 30.2008 — TJ111, if I might bother you for a bit more guidance. My menu is working, but only partially. Seems that only one subcategory is showing up per category, when there should be up to 7 or so in some. I think I may need another query to get the subcategories for any given category, but am unsure how to integrate it.
[CODE] $catmenu = mysql_query("SELECT subcategory, category FROM $DBTABLE1 ORDER BY category, subcategory");

$e=1;
$se=0;
while($row = mysql_fetch_array($catmenu))
{
$CAT=$row['category'];
$SUBCAT=$row['subcategory'];
$encCAT=htmlentities($CAT);
$encSUBCAT=htmlentities($SUBCAT);
if ($CAT != $prev_row)
{$se++;
echo "<br><font face="Arial, Helvetica, sans-serif" size=2><b><a onClick="expandcontent('sc$e')"><u>sc$e $CAT</u></a></b></font><br>";
$e++;
echo "<div id="sc$se" class="switchcontent">&nbsp;&nbsp;<font face="Arial, Helvetica, sans-serif" size=2><a href="http://www.navi-gator.com/demo/bg-testing/sfm/products.php?cat=$encCAT&subcat=$encSUBCAT">sc$se $SUBCAT</a></font></div>";
}
$prev_row=$CAT;
}[/CODE]
Copy linkTweet thisAlerts:
@TJ111Jan 30.2008 — Can you give me an example of the html this outputs?
Copy linkTweet thisAlerts:
@stmosaicauthorJan 30.2008 — sure. When you click on Category Number, menu of subcategories that go with that Category shold become visible. Category One has 6 subcats

sc1 Category One

sc1 DS9

sc2 Category Two


sc2 B5

sc3 Category Three

sc3 Misc

Want to try the working model. here's the url:http://www.navi-gator.com/demo/bg-testing/sfm/products.php?cat=Sci-Fi&subcat=B5
Copy linkTweet thisAlerts:
@stmosaicauthorFeb 04.2008 — Still unable to get the JS & Php to play nice together. I've tried various solutions, with no success. After days of working on this, I am extremely frustrated and could really use some more help.

Here is my current code:

Above header: [CODE]
$catmenu = mysql_query("SELECT subcategory, category FROM $DBTABLE1 ORDER BY category, subcategory");
$e=1; [/CODE]


In body:[CODE]
while($row = mysql_fetch_array($catmenu))
{
$CAT=$row['category'];
$SUBCAT=$row['subcategory'];
$encCAT=htmlentities($CAT);
$encSUBCAT=htmlentities($SUBCAT);
$CAT=$row['category'];
$SUBCAT=$row['subcategory'];
$encCAT=htmlentities($CAT);
$encSUBCAT=htmlentities($SUBCAT);

if ($CAT != $prev_row)
{
echo "<br><font face="Arial, Helvetica, sans-serif" size=2><b><a onClick="expandcontent('$e')"><u>$e $CAT</u></a></b></font><br>";
$e++;

$s=$e - 1;
echo "<div id="$s" class="switchcontent">";
}
if ($SUBCAT != $prev_sub)
{
echo "&nbsp;&nbsp;<font face="Arial, Helvetica, sans-serif" size=2><a href="http://www.navi-gator.com/demo/bg-testing/sfm/products.php?cat=$encCAT&subcat=$encSUBCAT">";

echo "$s $SUBCAT</div></a></font>";
}
$prev_row=$CAT;
$prev_sub=$SUBCAT;
}
?>[/CODE]

Current html output:

1 Category one

1 Subcat One

1 Subcat two

1 Subcat three

1 Subcat four

2 Category two

2 Subcat one

2 Subcat two

2 Subcat three

etc. Contractibility is controlled by the onclick & the div tags. The only subcategories contracting are the 1st one of each category. Any ideas?
Copy linkTweet thisAlerts:
@stmosaicauthorFeb 04.2008 — A breakthrough! I moved the ending div tag for switchcontent to the beginning of the if statement

( if ($CAT != $prev_row)

{

echo "</div><br><font face="Arial, Helvetica, sans-serif" size=2><b><a onClick="expandcontent('$e')">)

and now it almost works. The only remaining issue is that the last category is not showing its subcategories. Any ideas on one last tweak that will get this working?
Copy linkTweet thisAlerts:
@stmosaicauthorFeb 04.2008 — In thanks to TJ111 for his help and this website for being such an invaluable resource, I'm posting the solution to the JS-Php collaspsible menu for others to use. Original JS was from Dynamic Drive. ?

[CODE]<?php
include("DB_connection");
$catmenu = mysql_query("SELECT subcategory, category FROM $DBTABLE1 ORDER BY category, subcategory");
$e=1;
?>

<HTML>
<HEAD>
<TITLE><menu></TITLE>
<script type="text/javascript">
/***********************************************
* Contractible Headers script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
var enablepersist="on" //Enable saving state of content structure? (on/off)

if (document.getElementById){
document.write('<style type="text/css">')
document.write('.switchcontent{display:none;}')
document.write('</style>')
}

function getElementbyClass(classname){
ccollect=new Array()
var inc=0
var alltags=document.all? document.all : document.getElementsByTagName("*")
for (i=0; i<alltags.length; i++){
if (alltags[i].className==classname)
ccollect[inc++]=alltags[i]
}
}

function contractcontent(omit){
var inc=0
while (ccollect[inc]){
if (ccollect[inc].id!=omit)
ccollect[inc].style.display="none"
inc++
}
}

function expandcontent(cid){
if (typeof ccollect!="undefined"){
contractcontent(cid)
document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "none"
selectedItem=cid+"|"+document.getElementById(cid).style.display
}
}

function revivecontent(){
selectedItem=getselectedItem()
selectedComponents=selectedItem.split("|")
contractcontent(selectedComponents[0])
document.getElementById(selectedComponents[0]).style.display=selectedComponents[1]
}

function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function getselectedItem(){
if (get_cookie(window.location.pathname) != ""){
selectedItem=get_cookie(window.location.pathname)
return selectedItem
}
else
return ""
}

function saveswitchstate(){
if (typeof selectedItem!="undefined")
document.cookie=window.location.pathname+"="+selectedItem
}

function do_onload(){
getElementbyClass("switchcontent")
if (enablepersist=="on" && getselectedItem()!="")
revivecontent()
}


if (window.addEventListener)
window.addEventListener("load", do_onload, false)
else if (window.attachEvent)
window.attachEvent("onload", do_onload)
else if (document.getElementById)
window.onload=do_onload

if (enablepersist=="on" && document.getElementById)
window.onunload=saveswitchstate
</script>
<STYLE type="text/css">
a.menu{text-decoration: none}
</STYLE>
</HEAD>
<body>
<table>
<tr>
<td width="200" valign="top">
<div align="center">
<p><font face="Arial, Helvetica, sans-serif" size="2"><b> Browse</b></font><br>
</div>
<?php
while($row = mysql_fetch_array($catmenu))
{
$CAT=$row['category'];
$SUBCAT=$row['subcategory'];
$encCAT=htmlentities($CAT);
$encSUBCAT=htmlentities($SUBCAT);

if ($CAT != $prev_row)
{
echo "</div><br><font face="Arial, Helvetica, sans-serif" size=2><b><a onClick="expandcontent('$e')">$e $CAT</a></b></font><br>";
$e++;

$s=$e - 1;
echo "<div id="$s" class="switchcontent">";

}

echo "&nbsp;&nbsp;<font face="Arial, Helvetica, sans-serif" size=2><a href="http://www.navi-gator.com/demo/bg-testing/sfm/products.php?cat=$encCAT&subcat=$encSUBCAT">";
echo "$s $SUBCAT</a></font><br>";
$prev_row=$CAT;
}
?>
</td></tr></table>[/CODE]
×

Success!

Help @stmosaic spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...